Reputation: 1276
I have a multi dimensional array like this ;
A: Array (
[0] => Array ( [id] => 1 [name] => name1 )
[1] => Array ( [id] => 2 [name] => name2 )
[2] => Array ( [id] => 3 [name] => name3 )
)
And I have a array like this;
B: Array (
[0] => Array ( [id] => 2 [name] => name2 )
)
How to get the position of $b
in $a
? I want an output like 0,1,2...
Else if B = name2
can I get the position from A as 0,1,2... ?
Upvotes: 1
Views: 31
Reputation: 3157
You can use array_search()
array_search — Searches the array for a given value and returns the first corresponding key if successful
<?php
$a= [
['id' => 1, 'name' => 'name1'],
['id' => 2, 'name' => 'name2'],
['id' => 3, 'name' => 'name3'],
];
$b= ['id' => 2, 'name' => 'name2'];
$index = array_search($b, $a);
echo $index;
?>
Output: 1
Since your needle position is in index 1 of array $a, you will get 1.
Upvotes: 1