Bunker Boy Bunker Boy - 29 days ago 6
PHP Question

how to remove an array from a multidimensional array?

I have a multidimensional which is a dynamic array like :

Array
(
[0] => Array
(
[key] => delete
[label] => hi Delete
)

[1] => Array
(
[key] => edit
[label] => hi Edit
)

[2] => Array
(
[key] => update
[label] => hi update
)

)


now i want to delete an array below from above multidimensional array:

Array
(
[key] => delete
[label] => hi Delete
)


finally i want an output like:

Array
(

[0] => Array
(
[key] => edit
[label] => hi Edit
)

[1] => Array
(
[key] => update
[label] => hi update
)

)


For this i have tried, below is my code:

<?php
$arr1 = array(array("key" => "delete", "label" => "hi Delete"),array("key" => "edit", "label" => "hi Edit"), array("key" => "update", "label" => "hi update"));
$diff = array_diff_assoc($arr1, array("key" => "delete", "label" => "hi Delete"));
print_r($diff);
?>


But i get full $arr1 in the output:

Array
(
[0] => Array
(
[key] => delete
[label] => hi Delete
)

[1] => Array
(
[key] => edit
[label] => hi Edit
)

[2] => Array
(
[key] => update
[label] => hi update
)

)


how can i do this please help me

Answer

Use array_filter with callback as

$arr1 = array_filter($arr1, function ($var) {
    return $var['key'] != 'delete';
});
print_r($arr1);
Comments