开发者

how to loop over this array to obtain only the number 6 and 2? [closed]

开发者 https://www.devze.com 2023-04-05 02:22 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by开发者_运维技巧
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by开发者_运维技巧 editing this post.

Closed 9 years ago.

Improve this question

I am trying to loop over the array called $res, but I want to obtain only the values 6 and 2, for each name it is an id, is its with a foreach o just a normal for? I am confused because the array has two arrays insiden and it increments if I add a new name like 'Joe'

$arrayDirectory = array('Nick', 'Alex');

$res = array();
foreach($arrayDirectory as $user) {
    $res[] = $obj->obtainID($user);
}

echo print_r($res);
Array ( [0] => Array ( [0] => Array ( [id_usuario] => 6 [0] => 6 ) ) [1] => Array ( [0] => Array ( [id_usuario] => 2 [0] => 2 ) ) ) 1


foreach ($res as $item) {
    echo $item[0][0];
}

Or

foreach ($res as $item) {
    echo $item[0]['id_usuario'];
}

Depending on what you are looking for

With PHP5.3 you can also use array_map() with a lambda

$idUsarios = array_map(function ($item) {
    return $item[0]['id_usario'];
}, $res);


Change

$res[] = $obj->obtainID($user);

to

$user = $obj->obtainID($user);
$res[] = $user[0]['id_usuario'];
0

精彩评论

暂无评论...
验证码 换一张
取 消