从嵌套的关联数组中使用其键删除数组

时间:2013-09-06 12:56:39

标签: php arrays

我有这个数组:

$array = array(
    "foo" => "bar",
    "barw"    => "21",
    "bara"    => "22",
    "barq"    => "25",
    "multix" => array(
         "1" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         ),
        "2" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         ),
    "893" => "bar",
    "563" => "bar",
    "hd8" => "bar",
    "multiv" => array(
         "1" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         ),
         "2" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         ),
    "tw" => "bar",
    "qa" => "bar",
    "op" => "bar"
    )
    )
);

我正在阅读和写作:

echo '<pre>';
print_r($array);
echo '</pre>';

echo "<br/>";
echo $array['multix']['1']['ar1'].'<br/>';
echo "<br/>";
echo $array['multix']['1']['ar2'].'<br/>';
echo "<br/>";
echo $array['multix']['1']['ar3'].'<br/>';
echo "<br/>";
echo $array['multix']['1']['ar4'].'<br/>';
$array['multix']['1']['ar4'] = "lego";

但是,我无法删除我选择的数组,如:

unset($array['multiv']['1']);

echo '<pre>';
print_r($array);
echo '</pre>';

如何使用密钥删除数组?

1 个答案:

答案 0 :(得分:3)

您的multiv数组位于multix数组中,因此您需要添加multix名称

unset($array['multix']['multiv']['1']);

如果multiv应该是索引级别的一个,那么你可能会错误地设置你的数组。