Tuesday 14 August 2018

Removing an item from an associative array in PHP by value

Recently, I was working with some JSON decoded into a PHP array, and I needed to remove an item from the associative array. With only the value
Recently, I was working with some JSON decoded into a PHP array, and I needed to remove an item from the associative array. With only the value, I wasn’t sure how to remove it, and here was the code I used.
foreach($arr['category'] as $key => $value) {
 if (in_array('item_to_remove', $value)) {
  unset($arr['category'][$key]);
 }
}

0 comments:

Post a Comment