Search code examples
array-unset

unset not deleting key in multidimensional array


I have multidimensional array and wants to remove delivery location where ever it exists

   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;

problem is it's returning above array as it is without removing any key from it.


Solution

  • Easy and fast to understand way

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