使用array_key_exists搜索多维数组不起作用

时间:2012-11-08 02:14:23

标签: php array-key-exists

我试着让if else()语句在执行之前查找2个或更多条件,但它对我不起作用。我的最终目标是搜索数组[Pack]并且如果[Type] == s45&& [packageA1]然后使用array_slice将其他信息添加到[Type array]

if()条件工作正常但不是else if()....

$节点:

Array
(
    [Pack] => Array
        (
            [Type] => s45
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s45 info here
                    [image] => 
                )

        )

    [Pack2] => Array
        (
            [Type] => s99
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s99 info goes here
                    [image] => 
                )

        )
)

//more code...
$i = 1;
if(!array_key_exists($item[0], $node)){
     $node[$row[0]] = array("Type" => $item[0], "package".$item[1] => array("level" => "REMOVE FROM DB", "stage" => "REMOVE FROM DB", "description" => $item[3], "image" => $item[4]));
}else if(array_key_exists($item[0], $node) && array_key_exists("package".$item[1], $node)){
     $i++;
     $res = array_slice($node[$rowKey], 0, $i, true) +  array("my_key" => "my_value") + array_slice($node[$rowKey], $i, count($node[$rowKey]) - 1, true);
}

预期产出:

Array
(
    [Pack] => Array
        (
            [Type] => s45
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s45 info here
                    [image] => 
                )
            [packageA2] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => blahhhhhh blah blahh
                    [image] => 
                )
            [packageE1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => moar random stuff inserted
                    [image] => 
                )    
        )

    [Pack2] => Array
        (
            [Type] => s99
            [packageA1] => Array
                (
                    [level] => REMOVE FROM DB
                    [stage] => REMOVE FROM DB
                    [description] => s99 info goes here
                    [image] => 
                )

        )
)

1 个答案:

答案 0 :(得分:0)

您可以使用array_filter

$filter = array_filter($data, function ($p) {
    return $p['Type'] == "s45" && is_array($p['packageA1']);
});


$packageA2 = array(
        'level' => 'REMOVE FROM DB',
        'stage' => 'REMOVE FROM DB',
        'description' => 'blahhhhhh blah blahh',
        'image' => NULL);

$packageE1 = array(
        'level' => 'REMOVE FROM DB',
        'stage' => 'REMOVE FROM DB',
        'description' => 'moar random stuff inserted',
        'image' => NULL,
);

$filter['Pack']['packageA2'] = $packageA2 ;
$filter['Pack']['packageE1'] = $packageE1 ;
var_dump($filter);

输出

array
  'Pack' => 
    array
      'Type' => string 's45' (length=3)
      'packageA1' => 
        array
          'level' => string 'REMOVE FROM DB' (length=14)
          'stage' => string 'REMOVE FROM DB' (length=14)
          'description' => string 's45 info here' (length=13)
          'image' => null
      'packageA2' => 
        array
          'level' => string 'REMOVE FROM DB' (length=14)
          'stage' => string 'REMOVE FROM DB' (length=14)
          'description' => string 'blahhhhhh blah blahh' (length=20)
          'image' => null
      'packageE1' => 
        array
          'level' => string 'REMOVE FROM DB' (length=14)
          'stage' => string 'REMOVE FROM DB' (length=14)
          'description' => string 'moar random stuff inserted' (length=26)
          'image' => null