Answer the question
In order to leave comments, you need to log in
How to find an element in a multidimensional array using array_search?
there is an array
Array
(
[0] => Array
(
[node] => Array
(
[id] => 17874292930344481
[text] =>
[created_at] => 1555015118
[did_report_as_spam] =>
[owner] => Array
(
[id] => 6773204196
[is_verified] =>
[profile_pic_url] =>
[username] => gadalka_lida999
)
[viewer_has_liked] =>
[edge_liked_by] => Array
(
[count] => 0
)
)
)
[1] => Array
(
[node] => Array
(
[id] => 18036829474119034
[text] => Her account please
[created_at] => 1555019066
[did_report_as_spam] =>
[owner] => Array
(
[id] => 4245266739
[is_verified] =>
[profile_pic_url] =>
[username] => el94__
)
[viewer_has_liked] =>
[edge_liked_by] => Array
(
[count] => 0
)
)
)
$key = array_search("gadalka_lida999", array_column($array, 'username'));
echo $arr[$key]["username"];
Answer the question
In order to leave comments, you need to log in
If it is not known where exactly the desired key-value pair can be located, then something like this:
function isItemExists(iterable $source, $expectedKey, $expectedValue): bool
{
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($source, RecursiveArrayIterator::CHILD_ARRAYS_ONLY),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $itemKey => $itemValue) {
if ($expectedKey === $itemKey && $expectedValue === $itemValue) {
return true;
}
}
return false;
}
Alternatively, traverse the array recursively. On each of the "levels" use array_search(). If found, exit.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question