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 questionI 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'];
精彩评论