Jump to content

Run through an array and delete certain entries?


bloodgoat

Recommended Posts

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?

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

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));

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.