Skip to content
Advertisement

How to remove specific element from array in php

I have the following array

Array
(
    [tags] => Array
        (
            [0] => hello
        )

    [assignee] => 60b6a8a38cf91900695dd46b
    [multiple_assignee] => Array
        (
            [0] => Array
                (
                    [accountId] => 60b6a8a38cf91900695dd46b
                )

            [1] => Array
                (
                    [accountId] => 5b39d23d32e26a2de15f174f
                )

        )

)

I want to remove 60b6a8a38cf91900695dd46b from the multiple_assignee array. I have tried with the following code:

if (($key = array_search($this->getUsersHashMapValue($responsiblePartyIds[0]), $mutipleAssignee)) !== false) {
                        unset($mutipleAssignee[$key]['accountId']);
                    }

But it is not removing that element. The intention is I don’t want to repeat the 60b6a8a38cf91900695dd46b assignee in the multiple assignee array.

I have also tried with the following code:

foreach($mutipleAssignee as $subKey => $subArray){
                        if($subArray['accountId'] == $this->getUsersHashMapValue($responsiblePartyIds[0])){
                            unset($mutipleAssignee[$subKey]);
                        }
                    }

But it is resulting as

Array
(
    [tags] => Array
        (
            [0] => hello
        )

    [assignee] => 60b6a8a38cf91900695dd46b
    [multiple_assignee] => Array
        (
            [1] => Array
                (
                    [accountId] => 5b39d23d32e26a2de15f174f
                )

        )

)

rather than

[multiple_assignee] => Array
            (
                [0] => Array
                    (
                        [accountId] => 5b39d23d32e26a2de15f174f
                    )
    
            )

Thank you

Advertisement

Answer

Just extract the accountId column and search that. Then use that key:

$key = array_search($this->getUsersHashMapValue($responsiblePartyIds[0]), 
                    array_column($mutipleAssignee, 'accountId'));
                    
unset($mutipleAssignee[$key]);

After your edit it seems you just want to reindex the subarray after unset:

$mutipleAssignee = array_values($mutipleAssignee);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement