3

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

8
  • You can try unset($arr1[0]); Commented Nov 3, 2016 at 12:15
  • here the array is dynamic i don't know the index Commented Nov 3, 2016 at 12:17
  • or check this stackoverflow.com/questions/369602/… Commented Nov 3, 2016 at 12:17
  • Take a look at this: php.net/manual/en/function.array-diff-assoc.php array_diff is working another way than you expect. Commented Nov 3, 2016 at 12:18
  • You are using arrray_diff_assoc($arr1, array("key" => "delete", "label" => "hi Delete")) here $arr1 is multidimension and next one is single dimension that's why you are getting same Commented Nov 3, 2016 at 12:20

2 Answers 2

3

Use array_filter with callback as

$arr1 = array_filter($arr1, function ($var) {
    return $var['key'] != 'delete';
});
print_r($arr1);
Sign up to request clarification or add additional context in comments.

3 Comments

but the order of index has not arranged properly, it delete the 0th index and starts array with 1index
OP mentioned 1,000's of records and seems concerned about performance, using php's array_ functions are much slower than interating through the collection.
@BunkerBoy then $arr1 = array_values($arr1);
1

You should loop through the array and test for the key you want to remove, like so: (written blind so youll need to test it!)

<?php
foreach ($arr1 as $thisArrIndex=>$subArray)
{
    if ( $subArray['key'] == "delete" )
    {
        unset($arr1[$thisArrIndex]);
    }
}
?>

Suggested edit was to break out of the loop after finding the key. It seems OP may have multiple keys like this (in the multiple sub arrays) and so i chose not to break out of the loop here.

3 Comments

i can not use loop @Stuart , is there any function by which i can do this?
Why can you not use a foreach...?
You could loop through 1,000,000 records using foreach in ~0.3 seconds... -- The answer by @Saty is good but performance is twice as slow as using foreach....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.