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;
            }   
          } 
Community
  • 1
  • 1
dot
  • 14,928
  • 41
  • 110
  • 218
  • 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. – Ian Atkin Jan 25 '13 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.. – dot Jan 25 '13 at 16:38

2 Answers2

1

Considering $array to be the outmost array:

foreach ($array as $a) {
    if ($a['Name'][0]) == 'test4') { ... }
}
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 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. :) – Ian Atkin Jan 25 '13 at 16:36
  • @GordonFreeman, yeah, but he said that the array *looks like that*. Therefore this is the most performant, simple and class-free solution. :) – Shoe Jan 25 '13 at 16:39
  • @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 Jan 25 '13 at 16:49
  • @dot, my example is 3 levels deep aswell: 1) `foreach` 2) `['Name']` 3) `[0]`. – Shoe Jan 25 '13 at 16:50
  • @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 – dot Jan 25 '13 at 16:54
  • @dot, that's exactly the same as mine, you just retrieving the first arrays keys in `$key` more. Glad it helped though. – Shoe Jan 25 '13 at 16:56
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!

}

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • thanks for this. I'll review to understand better. but how would I unset the record once I find it? – dot Jan 25 '13 at 17:02