yudijohn
yudijohn

Reputation: 1278

PHP Remove Multidimensional Array Value

I have array multidimensional code like this:

$array = [
    'fruits' => ['apple','orange','grape', 'pineaple'],
    'vegetables' => ['tomato', 'potato']
];
$eaten = 'grape';
unset($array[$eaten]);

and what i need is to delete 'grape' from the array because 'grape' already eaten. how to fix my code to unset the 'grape'?

and my question number two, if it can be unset, is there a way to unset multi value like

unset($array,['grape','orange']);

thanks for help..

Upvotes: 0

Views: 211

Answers (3)

Dwijen
Dwijen

Reputation: 590

This is how I would do it.

$array = [
    'fruits' => ['apple','orange','grape', 'pineaple'],
    'vegetables' => ['tomato', 'potato']
];

$unset_item = 'grape';

$array = array_map(function($items) use ($unset_item) {
    $found = array_search($unset_item, $items); 
        if($found){
            unset($items[$found]);
        }       
    return $items;
}, $array);

Upvotes: 0

devpro
devpro

Reputation: 16117

Instead of using unset() i suggest you to create a new Array after removal of required value benefit is that, your original array will remain same, you can use it further:

Example:

// your array
$yourArr = array(
                'fruits'=>array('apple','orange','grape', 'pineaple'),
                'vegetables'=>array('tomato', 'potato')
                );
// remove array that you need
$removeArr = array('grape','tomato');

$newArr = array();
foreach ($yourArr as $key => $value) {
    foreach ($value as $finalVal) {
        if(!in_array($finalVal, $removeArr)){ // check if available in removal array
            $newArr[$key][] = $finalVal;
        }       
    }   
}

echo "<pre>";
print_r($newArr);

Result:

Array
(
    [fruits] => Array
        (
            [0] => apple
            [1] => orange
            [2] => pineaple
        )

    [vegetables] => Array
        (
            [0] => potato
        )

)

Explanation:

Using this array array('grape','tomato'); which will remove the value that you define in this array.

Upvotes: 1

Parixit
Parixit

Reputation: 3855

You can remove eaten element by following way. Use array_search() you can find key at the position of your eaten element.

Here below code shows that in any multidimensional array you can call given function.

$array = [
    'fruits' => ['apple','orange','grape', 'pineaple'],
    'vegetables' => ['tomato', 'potato']
   ];
$eaten = 'grape';
$array = removeElement($array, $eaten);


function removeElement($data_arr, $eaten)
{
    foreach($data_arr as $k => $single)
    {
        if (count($single) != count($single, COUNT_RECURSIVE)) 
        {
           $data_arr[$k] = removeElement($single, $eaten);
        }
        else
        {
            if(($key = array_search($eaten, $single)) !== false)
            {
               unset($data_arr[$k][$key]);
            }
        }
    }
    return $data_arr;
}

P.S. Please note that you can unset() multiple elements in single call. But the way you are using unset is wrong.

Upvotes: 2

Related Questions