0

I'm trying to search through a multi-dimensional array for a value called "test4". The array looks like this:

Array
(
    [0] => Array
        (
            [VlanId] => Array
                (
                    [0] => 2
                )

            [Name] => Array
                (
                    [0] => test2
                )

        )

    [1] => Array
        (
            [VlanId] => Array
                (
                    [0] => 3
                )

            [Name] => Array
                (
                    [0] => test3
                )

        )

    [2] => Array
        (
            [VlanId] => Array
                (
                    [0] => 4
                )

            [Name] => Array
                (
                    [0] => test4
                )

        )

I found the following posts: Search a multidimensional array php

and

using array_search() to find values in php

and I'm using the rescursiveiterator method to find the value test4. My code looks like this:

foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($vlans)) as $key=>$value) {
    if ($value == 'test4') {
             print 'fount it. the key is: '. $key .' and value is: '. $value;
        break;
    }
}

This gives the following output:

fount it. the key is: 0 and value is: test4

I can't use this to unset the test4 record because [0] just unsets the first item in the outtermost array... in this case, it would remove VlanID 2, with the name test2.

can you help me figure out how to remove the record test4, once I've found it? I tried reading the following post:

Unsetting multi-dimensional arrays in php

but wasn't able to quite understand how to resolve this issue.

Thanks.

EDIT 1:

        foreach ($vlans as $a=>$value) {
            if ($value['Name'][0] =='test4' ){
                echo 'found it at: '. $value;
                unset($vlans[$a]);
                break;
            }   
          } 
2
  • Obviously, you need to look elsewhere as RecursiveIteratorIterator does not do what you want. The actual key is [2]['Name'][0], but you knew that already. Commented Jan 25, 2013 at 16:35
  • actually, my understanding is that RecursiveIteratorIterator does do what I want because it finds the value i'm looking for... It's the unset part that i need to figure out. ?? maybe I'm wrong.. Commented Jan 25, 2013 at 16:38

2 Answers 2

1

Considering $array to be the outmost array:

foreach ($array as $a) {
    if ($a['Name'][0]) == 'test4') { ... }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Works in the case given, but makes a bad, bad assumption about the nature of the array being searched. But... it might just be the answer. :)
@GordonFreeman, yeah, but he said that the array looks like that. Therefore this is the most performant, simple and class-free solution. :)
@Jeffrey. Actually, I think your example is missing an array...it's nested 3 levels in. But! Thank you! I tweaked your example... and have resolved my issue. The answer I was looking for is how to unset the record in the array... but your comment has tipped me off.
@dot, my example is 3 levels deep aswell: 1) foreach 2) ['Name'] 3) [0].
@Jeffrey...hm... with your exmaple, i was never getting it to find the record. check out my edit 1 in post - i've added my solution
|
1

This is a more robust solution that will work on any multi-dimensional array and return an array of the key path. It searches $haystack for $needle and returns an array of the key path if found in the array, or it returns false if not.

function arraySearchRecursive($needle, $haystack, $strict=false, $path=array()) {
    if(!is_array($haystack)) {
        return false;
    }

    foreach ($haystack as $key => $val) {
        if(is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path)) {
            $path = array_merge($path, array($key), $subPath);
            return $path;
        } elseif ((!$strict && $val == $needle) || ($strict && $val === $needle)) {
            $path[] = $key;
            return $path;
        }
    }

    return false; // value not in array!

}

1 Comment

thanks for this. I'll review to understand better. but how would I unset the record once I find it?

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.