BerbBrown Posted January 13, 2014 Share Posted January 13, 2014 Hi All I have the following multi-dimensional array and I would like to remove all the 'VOID' elements. I would like to maintain the array indexes. If I had to I would like to know how I can reset the indexes. Thank you very much in advance. BB Array([0] => Array ( [0] => VOID [1] => VOID [2] => VOID [3] => VOID [4] => VOID [5] => 16 [6] => VOID [7] => 11 )[1] => Array ( [0] => 21 [1] => 2 [2] => 2 [3] => 1 [4] => 33 [5] => 27 [6] => 32 [7] => VOID )[2] => Array ( [0] => VOID [1] => VOID [2] => VOID [3] => VOID [4] => VOID [5] => VOID [6] => VOID [7] => VOID )[3] => Array ( [0] => VOID [1] => VOID [2] => VOID [3] => VOID [4] => 131 [5] => 117 [6] => 183 [7] => VOID )[4] => Array ( [0] => VOID [1] => VOID [2] => VOID [3] => VOID [4] => VOID [5] => 41 [6] => 1 [7] => VOID )) Quote Link to comment https://forums.phpfreaks.com/topic/285318-remove-an-element-from-a-multi-dimensional-array/ Share on other sites More sharing options...
objnoob Posted January 13, 2014 Share Posted January 13, 2014 if the values are string void <?php foreach($yourArray as $k0=>$innerArray){ foreach($innerArray as $k1=>$value){ if(strtolower($value) === 'void') unset($yourArray[$k0][$k1]); } # will reindex array: $yourArray[$k0] = array_values($yourArray[$k0]); } if the values are actually null <?php foreach($yourArray as $k0=>$innerArray){ foreach($innerArray as $k1=>$value){ if($value == null) unset($yourArray[$k0][$k1]); } # will reindex array: $yourArray[$k0] = array_values($yourArray[$k0]); } You could also look into the array_filter() function http://us2.php.net/array_filter Quote Link to comment https://forums.phpfreaks.com/topic/285318-remove-an-element-from-a-multi-dimensional-array/#findComment-1465004 Share on other sites More sharing options...
mac_gyver Posted January 13, 2014 Share Posted January 13, 2014 i would write a call-back function to unset() the element based on the value, whatever it actually is, and use array_walk_recursive() to apply that function to every level/element of the array. Quote Link to comment https://forums.phpfreaks.com/topic/285318-remove-an-element-from-a-multi-dimensional-array/#findComment-1465005 Share on other sites More sharing options...
mac_gyver Posted January 13, 2014 Share Posted January 13, 2014 edit to the above: because of the way unset() works, what i suggested above won't work without using the $GLOBALS array, which we should probably forget exists. Quote Link to comment https://forums.phpfreaks.com/topic/285318-remove-an-element-from-a-multi-dimensional-array/#findComment-1465009 Share on other sites More sharing options...
mac_gyver Posted January 13, 2014 Share Posted January 13, 2014 assuming your void are actually nulls and the data is in $data - $data = array_map('array_filter',$data); Quote Link to comment https://forums.phpfreaks.com/topic/285318-remove-an-element-from-a-multi-dimensional-array/#findComment-1465117 Share on other sites More sharing options...
Barand Posted January 13, 2014 Share Posted January 13, 2014 if they are string value 'VOID' foreach ($data as $arr) { $newdata[] = array_values(array_filter($arr, function($a){return $a != 'VOID';})); } $newdata = array_filter($newdata); Quote Link to comment https://forums.phpfreaks.com/topic/285318-remove-an-element-from-a-multi-dimensional-array/#findComment-1465127 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.