I have an array:
Array
(
[user1] => Array
(
[id] => 1
[name] => 'john'
[types] => null
)
[user2] => Array
(
[id] => 2
[name] => 'jane'
[types] => Array(
[t_id] => 2
)
)
[user3] => Array
(
[id] => 3
[name] => 'jeff'
[types] => null
)
[user4] => Array
(
[id] => 4
[name] => 'steve'
[types] => Array(
[t_id] => 1
)
)
[user5] => Array
(
[id] => 5
[name] => 'rob'
[types] => Array(
[t_id] => 2
)
)
I need to find out the first user who has a t_id of 1 and the first user who has a t_id of 2
So in the above Jane would be the first user who has a t_id of 2 and Steve would be the first user who has a t_id of 1.
I know I could loop through the array:
private $tid1;
private $tid2;
foreach($data as $_v) {
if($_v['types']['t_id'] === 1) $tid1 = $_v;
if($_v['types']['t_id'] === 2) $tid2 = $_v;
}
This seems inefficient though, and the above would not work fully as the loop would keep going, and later occurrences of the t_id would replace the variable.
Is there a more efficient way to do this?
You can stop the loop when you found your td's: