Wednesday, March 15, 2017

How to delete an array inside an array based on value

I have an array in session

array:7 [▼
  0 => array:2 [▼
    "store" => "store1"
    "product" => "1"
  ]
  1 => array:2 [▼
    "store" => "store2"
    "product" => "2"
  ]
  2 => array:2 [▼
    "store" => "store3"
    "product" => "4"
  ]
]

I made a function that to remove arrays that matches the value of store when given. for instance I give store1 it should remove store1 array and outputs like this

array:7 [▼
  0 => array:2 [▼
    "store" => "store2"
    "product" => "2"
  ]
  1 => array:2 [▼
    "store" => "store3"
    "product" => "4"
  ]
]

Instead I get the output

array:2 [▼
  1 => "store2"
  2 => "store3"
]

My function

function removeFromSessionArray($name, $value)
{
    return session()->put($name, array_diff(session()->get('stores'), [$value]));
}

Can someone tell how can me achieve the possible output?

PS. Learning arrays.



via Alen

Advertisement