Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.