Reputation: 305
If there is a way by which array_search()
would return key of its first conflict like if I run
$key = array_search(40489, array_column($userdb, 'uid'));
on
Array
(
(1) => Array
(
(uid) => '5465',
(name) => 'Stefanie Mcmohn',
(pic_square) => 'urlof100'
),
(2) => Array
(
(uid) => '40489',
(name) => 'Michael',
(pic_square) => 'urlof40489'
)
);
it would ideally return
2
but I want it to return
1
i.e the first element that did not have the 'uid' = 40489 &
If its not possible with array_search()
is there any other way to do it with loops? I tried array_filter()
but can't get it to work.
Upvotes: 0
Views: 523
Reputation: 54831
As said in array_search
description:
Searches the array for a given value and returns the first corresponding key if successful
Returns the key for needle if it is found in the array, FALSE otherwise.
So, you cannot use array_search
to search something that not equals what you need. Instead write you own function, for example:
$array = []; // your array
foreach ($array as $key => $value) {
if ($value['uid'] != '40489') {
echo 'Key: ', $key;
// use `break` to stop iterating over
// array as you already found what you need
break;
}
}
Upvotes: 1