[英]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.我想从 multiple_assignee 数组中删除
60b6a8a38cf91900695dd46b
。 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.目的是我不想在多个受让人数组中重复
60b6a8a38cf91900695dd46b
受让人。
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谢谢
Just extract the accountId
column and search that.只需提取
accountId
列并进行搜索。 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
:编辑后,您似乎只想在
unset
之后重新索引子数组:
$mutipleAssignee = array_values($mutipleAssignee);
I would just use a simple for
loop.我只会使用一个简单
for
循环。 All of the array_*
functions are really helpful but I find that they hide nuances.所有的
array_*
函数都非常有用,但我发现它们隐藏了细微差别。 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此处演示: https://3v4l.org/tYppK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.