bloodgoat Posted June 20, 2009 Share Posted June 20, 2009 Is this possible? The array format is like this: <?php $array = array(); ?> <?php $array[] = array("value 1"); ?> <?php $array[] = array("value 2"); ?> <?php $array[] = array("value 3"); ?> How would I, strictly with PHP and no databasing, sniff through the array to find, "value 2" for example, and use fwrite to abolish it? Quote Link to comment https://forums.phpfreaks.com/topic/163047-run-through-an-array-and-delete-certain-entries/ Share on other sites More sharing options...
mattal999 Posted June 20, 2009 Share Posted June 20, 2009 Try this: <?php $array = array(); ?> <?php $array[] = array("value 1"); ?> <?php $array[] = array("value 2"); ?> <?php $array[] = array("value 3"); ?> <?php function remove_element($arr, $val){ foreach ($arr as $key => $value){ if ($arr[$key] == $val){ unset($arr[$key]); } } return $arr = array_values($arr); } remove_element($array, "value 2"); ?> Source: http://www.trap17.com/index.php/Remove-Php-Array-Based_t55677.html Quote Link to comment https://forums.phpfreaks.com/topic/163047-run-through-an-array-and-delete-certain-entries/#findComment-860301 Share on other sites More sharing options...
Maq Posted June 20, 2009 Share Posted June 20, 2009 Do you want to create a 2D array? You can just put them all into a single array, put the values you want to delete in another array, and use the function array_diff to 'abolish' them. i.e. $array = array("value 1","value 2","value 3"); $array2 = array("value 2"); print_r(array_diff($array, $array2)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/163047-run-through-an-array-and-delete-certain-entries/#findComment-860302 Share on other sites More sharing options...
bloodgoat Posted June 20, 2009 Author Share Posted June 20, 2009 Try this: <?php $array = array(); ?> <?php $array[] = array("value 1"); ?> <?php $array[] = array("value 2"); ?> <?php $array[] = array("value 3"); ?> <?php function remove_element($arr, $val){ foreach ($arr as $key => $value){ if ($arr[$key] == $val){ unset($arr[$key]); } } return $arr = array_values($arr); } remove_element($array, "value 2"); ?> Source: http://www.trap17.com/index.php/Remove-Php-Array-Based_t55677.html Thanks, I'll bookmark that and try it. Quote Link to comment https://forums.phpfreaks.com/topic/163047-run-through-an-array-and-delete-certain-entries/#findComment-860311 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.