Foreach within Foreach PHP

5

I have the following code:

$row = $twitterusers->selectUserAll();

foreach ($row as $fetch) {
    $users[] = $fetch;
}

var_dump($users);

It takes all the users of the table, however when using var_dump I realize that it has array inside array , see:

0 => 
    array (size=6)
      'id' => string '1' (length=1)
      'user_id' => string '123' (length=18)
      'screen_name' => string 'login1' (length=13)
      'oauth_token' => string 'token-hash' (length=50)
      'oauth_token_secret' => string 'token-hash2' (length=45)
      'vip' => string '0' (length=1)
  1 => 
    array (size=6)
      'id' => string '2' (length=1)
      'user_id' => string '1234' (length=18)
      'screen_name' => string 'login2' (length=12)
      'oauth_token' => string 'token-hash' (length=50)
      'oauth_token_secret' => string 'token-hash2' (length=45)
      'vip' => string '0' (length=1)

I wanted to get all the users_id , but I can not, for example:

Instead of 0 => and 1 => , I would like everything to be in an array only, how can I do that?

Note: print all ids as follows:

user_id => 123
user_id => 1234
user_id => 1235
user_id => 1236
    
asked by anonymous 25.07.2017 / 14:45

1 answer

5

You can do this:

$fetch = $twitterusers->selectUserAll();

foreach ($fetch as $row) {
    $usersIds[] = $row['id'];
}

var_dump($usersIds);

Or better yet, without having to iterate through array_column :

$fetch = $twitterusers->selectUserAll();

$userIds = array_column($fetch, 'id');

var_dump($usersIds);
    
25.07.2017 / 14:52