gpt4 book ai didi

php - 删除关联数组中的父元素

转载 作者:搜寻专家 更新时间:2023-10-31 20:57:16 24 4
gpt4 key购买 nike

问题:

我想用条件删除数组的父元素。

输入

$myArray = array (
0 =>
array (
'ulStatic' =>
array (
0 =>
array (
'day' => '2019-03-30 18:30:00',
'id' => '7',
'origin' => 'intentions',
),
),
'ulDynamic' =>
array (
0 =>
array (
'day' => '2019-03-30 18:30:00',
'id' => '5',
'origin' => 'intentions',
),
),
'ulCreatedDynamic' =>
array (
),
),
1 =>
array (
'ulStatic' =>
array (
0 =>
array (
'day' => '2019-03-31 09:30:00',
'id' => '8',
'origin' => 'intentions',
),
),
'ulDynamic' =>
array (
),
'ulCreatedDynamic' =>
array (
),
),
2 =>
array (
'ulStatic' =>
array (
),
'ulDynamic' =>
array (
),
'ulCreatedDynamic' =>
array (
0 =>
array (
'day' => '2019-04-02 11:15:00',
'id' => '275',
'origin' => 'obseques',
),
1 =>
array (
'day' => '2019-04-02 11:15:00',
'id' => '277',
'origin' => 'obseques',
),
),
),
);

在此数组中,我想在名为 ulCreatedDynamic 的关联数组中删除包含 id = 275day = '2019-04-02 11 的键:15:00';

因此必须删除此 key :

0 =>
array (
'day' => '2019-04-02 11:15:00',
'id' => '275',
'origin' => 'obseques',
),

尝试:

$id = 275;
$day = "2019-04-02 11:15:00";

foreach($myArray as $array):
foreach($array as $ul => $v):
if($ul == "ulCreatedDynamic"):
foreach($v as $elementKey => $element):
if($element['day'] == $day && $element['id'] == $id):
unset($v[$elementKey]);
endif;
endforeach;
endif;
endforeach;
endforeach;

我该如何解决这个问题?

最佳答案

foreach($some_array as $item) 将在任何项目发生更改时立即复制整个数组。您需要使用 & 运算符来处理引用。

foreach($myArray as &$array):
foreach($array as $ul => &$v):

关于php - 删除关联数组中的父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55436644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com