douxu0550
douxu0550
2013-01-25 16:31

使用array_search和unset()与多维数组

已采纳

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条回答

  • dougongyou7364 dougongyou7364 8年前

    Considering $array to be the outmost array:

    foreach ($array as $a) {
        if ($a['Name'][0]) == 'test4') { ... }
    }
    
    点赞 评论 复制链接分享
  • dongmeng9048 dongmeng9048 8年前

    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!
    

    }

    点赞 评论 复制链接分享