0

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

2

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.