How to remove values from an array in PHP?
In this post, we help you how to remove values from an array in PHP.
1. Code snippet
<?php function removeValueArray(&$valueInput = null, $key = ""){ if (is_array($valueInput)) { foreach ($valueInput as $key => $value) { $valueInput[$key] = removeValueArray($value, $key); } if (isset($key) || trim($key) != '') { return $valueInput; } } else { if (isset($key) || trim($key) != '') { return null; } } } $arr = array(1, 3, 4, 2, 22); echo "Array Before remove value:"; print_r($arr); echo "<br/>"; echo "Array After remove value:"; $arr = removeValueArray($arr); print_r($arr); ?>
Output
Array Before remove value:Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 2 [4] => 22 ) Array After remove value:Array ( [0] => [1] => [2] => [3] => [4] => )
2. Example
Input:
array(1, 3, 4, 2, 22);
Output:
Array ( [0] => [1] => [2] => [3] => [4] => )
3. Download this Example from Github
Have a nice day!
SolTuts 🙂