PHP array delete using value not using key of array

PHP array delete using value not using key of array

hear we will show you how you PHP array delete using value not using key of array. hear we use different method for delete array value.

How to delete an element from an array in php?

When deleting an element from an array in PHP, a good function to use is the unset function. Here is an example of its usage. An example of using unset to delete an element from an array. we show you the simplest function to perform PHP array delete using value not using key of array.

METHOD 1 :: PHP array delete using value

Using array_search() and unset, try the following:

$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];
$delete_your_val  = 9999;
if(($key = array_search($delete_your_val, $your_array)) !== false) {
unset($your_array[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a false-y value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.

The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

METHOD 2 :: PHP array delete using value

One interesting way is using using array_keys():

$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];
$delete_your_val  = 9999;
foreach (array_keys($your_array, $delete_your_val, true) as $key)
{
unset($your_array[$key]);
}

The array_keys() function takes two additional parameters to return only keys for a particular value and whether strict checking is required (i.e. using === for comparison).

This can also remove multiple array items with the same value (e.g. [1, 2, 3, 3, 4]).

METHOD 3 :: PHP array delete using value

If you know for definite that your array will contain only one element with that value, you can do

$delete_your_val  = 9999;
$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];
$key = array_search($delete_your_val, $array);
if (false !== $key) {
unset($array[$key]);
}

METHOD 3 :: PHP array delete using value

If, however, your value might occur more than once in your array, you could do this

$delete_your_val  = 9999;
$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];
$your_array = array_filter($your_array, function($e) use ($delete_your_val)
{
return ($e !== $delete_your_val);
});

Note: The second option only works for PHP5.3+

METHOD 4 :: PHP array delete using value

$delete_your_val  = 9999;
$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];

$your_array = array_flip($your_array);
unset($your_array[$delete_your_val]);
$your_array = array_flip($your_array);

METHOD 5 :: PHP array delete using value

Have a look at following code:

$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];

You can do:

$delete_your_val1  = 9999;
$delete_your_val2  = 8888;
$your_array = array_diff($your_array, array($delete_your_val1, $delete_your_val2));

And that will get you this array:

[1111, 2222, 3333, 4444, 5555, 66666, 7777]

METHOD 6 :: PHP array delete using value

using the following code, the repetitive values will be removed from the $your_array.

$delete_your_val = 9999;
$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];
$your_array = array_diff($your_array, array($delete_your_val));

METHOD 7 :: PHP array delete using value

To delete multiple values try this one

$delete_your_val  = 9999;
$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];
while (($key = array_search($delete_your_val, $your_array)) !== false)
{
unset($your_array[$key]);
}

METHOD 8 :: PHP array delete using value

we can also do method. Explanation: Delete the element that has the key 9999 after flipping the array.

$delete_your_val  = 9999;
$your_array = [1111, 2222, 3333, 4444, 5555, 66666, 7777, 8888, 9999];

unset($your_array[array_flip($your_array)[$delete_your_val]]);

Leave a Comment

Your email address will not be published. Required fields are marked *

38  +    =  44

We accept paid advance and paid guest Posting on our Site : Contact US