未设置不删除多维数组中的键

时间:2019-05-09 07:33:01

标签: array-unset

我有多维数组,想删除交货位置

   Array
     (
     [0] => Array
      (
        [amountReceived] => 1
        [deliveryLocation] => germany
      )

      [1] => Array
       (
        [amountReceived] => 2
        [deliveryLocation] => bulgaria
       )
     )

PHP

     foreach ($arr as $val) 
      {
        foreach($val as $k => $v)
        {
            if($k == 'deliveryLocation')
            {
                unset($arr[$k]);
            }
        }
      }

      return $arr;

问题是它会按原样返回数组上面,而不会从中删除任何键。

1 个答案:

答案 0 :(得分:0)

易于理解的方式

$t=0;
foreach ($arr as $val)
{
      unset($arr[$temp]['deliveryLocation']);
      $t++;
}