0

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

8
  • So is $mutipleAssignee the array that you show or only the multiple_assignee portion? Commented Jun 7, 2021 at 16:24
  • yes, as per shown in the structure, I just want to remove [0] => Array ( [accountId] => 60b6a8a38cf91900695dd46b ) from there, as this value exists as [assignee] => 60b6a8a38cf91900695dd46b Commented Jun 7, 2021 at 16:27
  • 1
    So you want it removed and you want the array reindexed to start at zero? Commented Jun 7, 2021 at 16:32
  • yes but don't know why it is starting from [1] rather than [0] Commented Jun 7, 2021 at 16:33
  • Do you really care? If you have two keys, alpha and beta, removing alpha doesn't automatically re-key everything, alpha is just gone and beta is still there. There isn't anything special about the first item by default being 0, that's just what PHP does in the absence of any additional information. I would encourage you to never used $array[0] syntax and instead look at reset or other functions to get "the first" item Commented Jun 7, 2021 at 16:36

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

after removing why it is starting from [1] => Array ( [accountId] => 5b39d23d32e26a2de15f174f ) rather than [0] => Array ( [accountId] => 5b39d23d32e26a2de15f174f )
Because you removed 0, it doesn't reindex, why would it? I edited...
it is showing Array ( [tags] => Array ( [0] => hello ) [assignee] => 60b6a8a38cf91900695dd46b [multiple_assignee] => Array ( ) ) as result
1

I would just use a simple for loop. All of the array_* functions are really helpful but I find that they hide nuances. Since I don't have your functions I'm just making a plain-old one, but you should be able to port this.

$data = [
    'tags' => [
        'hello',
    ],
    'assignee' => '60b6a8a38cf91900695dd46b',
    'multiple_assignee' => [
        [
            'accountId' => '60b6a8a38cf91900695dd46b',
        ],
        [
            'accountId' => '5b39d23d32e26a2de15f174f',
        ],
    ],
];


$assignee = $data['assignee'];
foreach ($data['multiple_assignee'] as $multiple_assignee_key => $multiple_assignee) {
    // if the root assignee is also listed in the multiple assignee area
    if ($multiple_assignee['accountId'] === $assignee) {
        // remove the duplicate from the multiple area
        unset($data['multiple_assignee'][$multiple_assignee_key]);
        
        // re-index the array
        $data['multiple_assignee'] = array_values($data['multiple_assignee']);
    }
}

This outputs:

array(3) {
  ["tags"]=>
  array(1) {
    [0]=>
    string(5) "hello"
  }
  ["assignee"]=>
  string(24) "60b6a8a38cf91900695dd46b"
  ["multiple_assignee"]=>
  array(1) {
    [0]=>
    array(1) {
      ["accountId"]=>
      string(24) "5b39d23d32e26a2de15f174f"
    }
  }
}

Demo here: https://3v4l.org/tYppK

Comments

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.