Mohsin
Mohsin

Reputation: 179

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.

Upvotes: 0

Views: 49

Answers (1)

Carlos Alves Jorge
Carlos Alves Jorge

Reputation: 1985

Easy and fast to understand way

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

Upvotes: 0

Related Questions