1

I want to find an array inside a multi dimensional array using PHP.

For example:

$arr = array(

    '1253' => array('0' => 'av/data'),
    '1254' => array('1' => 'celling', '2' => 'electrical'),
    '1255' => array('1' => 'celling', '2' => 'electrical'),
);

Want to search for

array('1' => 'celling', '2' => 'electrical')

expected result should be:

array('1254', '1255')

Need a way that will return the match index, just like _.findIndex in lodash Javascript library.

4
  • Did you give up??? Commented Jul 3, 2017 at 17:49
  • Yeap, almost :) Commented Aug 2, 2017 at 23:20
  • So what does that mean? Still having issues? Commented Aug 3, 2017 at 13:41
  • Actually I have created my own method in PHP using recursion. That solve my problem for now. Commented Aug 9, 2017 at 23:40

2 Answers 2

2

array_search() and array_keys() can take an array as the needle. array_search() returns the first matching key and array_keys() will return all matching keys:

$keys = array_keys($arr, array('1' => 'celling', '2' => 'electrical'));
Sign up to request clarification or add additional context in comments.

Comments

1

Loop the main array and compare it with search array.

 $arr = array(

    '1253' => array('0' => 'av/data'),
    '1254' => array('1' => 'celling', '2' => 'electrical'),
    '1255' => array('1' => 'celling', '2' => 'electrical'),
);

$array_check = array('1' => 'celling', '2' => 'electrical');

foreach($arr as $key=>$val){

    if($val === $array_check){
        $new_array[]=$key;
    }
}

print_r($new_array);

1 Comment

This works fine. Actually i was looking for some build-in function.

Your Answer

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.