根据数组键删除内部数组

时间:2019-01-04 19:05:22

标签: php multidimensional-array

我有一个多维数组,需要从数组中删除一个项目。我希望删除整个子集数组,而不仅仅是一个值。我希望将其作为子集数组的键。我已经尝试过多次尝试,以使用循环和其他数组函数来删除此错误,并在堆栈中进行搜索而没有成功。

这是数组的一个例子。

$ array

Array ( 
    [key1] => Array ( [title] => Title [visible] => 1 [order] => 0 ) 
    [key2] => Array ( [title] => Title [visible] => 1 [order] => 1 ) 
    [key3] => Array ( [title] => Title [visible] => 1 [order] => 2 ) 
    [key4] => Array ( [title] => Title [visible] => 1 [order] => 3 ) 
    [key5] => Array ( [title] => Title [visible] => 1 [order] => 4 )
    [key6] => Array ( [title] => Title [visible] => 1 [order] => 5 ) 
    [key7] => Array ( [title] => Title [visible] => 1 [order] => 6 ) 
)

我正在使用php7

当我尝试循环时,它只会删除键,并保留其余值。

我要删除key4子数组,并保持其余部分不变。

预期输出:

Array ( 
    [key1] => Array ( [title] => Title [visible] => 1 [order] => 0 ) 
    [key2] => Array ( [title] => Title [visible] => 1 [order] => 1 ) 
    [key3] => Array ( [title] => Title [visible] => 1 [order] => 2 )  
    [key5] => Array ( [title] => Title [visible] => 1 [order] => 4 )
    [key6] => Array ( [title] => Title [visible] => 1 [order] => 5 ) 
    [key7] => Array ( [title] => Title [visible] => 1 [order] => 6 ) 
)

1 个答案:

答案 0 :(得分:0)

在此处删除数组基本键

tsconfig.json

);

tsconfig.json

此处输出

在数组之前

$res= Array ( 
'key1' => Array ( 'title' => 'Title', 'visible' => 1 ,'order' => 0 ) ,
'key2' => Array ( 'title' => 'Title' ,'visible' => 1 ,'order' => 1 ) ,
'key3' => Array ( 'title' => 'Title' ,'visible' => 1 ,'order' => 2 ) ,
'key4' => Array ( 'title' => 'Title' ,'visible' => 1 ,'order' => 3 ) ,
'key5' => Array ( 'title' => 'Title' ,'visible' => 1 ,'order' => 4 ),
'key6' => Array ( 'title' => 'Title' ,'visible' => 1 ,'order' => 5 ) ,
'key7' => Array ( 'title' => 'Title' ,'visible' => 1 ,'order' => 6 ) ,

数组之后

echo "<pre>";
echo "Before Array<br>";
print_r($res);
unset($res['key4']);
echo "After Array<br>";
print_r($res);