0
$array = [
 [
 'name' => 'john',
 'age' => 25
 'children' => [
 'jane',
 'jeff',
 ],
 ],
 ....
];

I wish to get the entire sub array depending on a value in a nested array, for example, I can do...

array_search('john', array_column($array, 'name')); // 0

But how can I do the same for the children sub array, for example, I want to find the john multidimensional array by searching for a child's name, e.g. jeff

asked Nov 28, 2022 at 17:30

1 Answer 1

1

Only the array_search function alone will probably not be enough. The combination array_search and array_filter would help you

array_filter($array, function($value) {
 return ($value['name'] === 'john' && in_array('jeff', $value['children']));
});
answered Nov 28, 2022 at 17:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.