gpt4 book ai didi

php - 将数据插入foreach中的关联数组

转载 作者:行者123 更新时间:2023-12-04 20:40:18 27 4
gpt4 key购买 nike

所以我有这个数组 $companies,它看起来像这样:

array(
(int) 0 => array(
'Company' => array(
'id' => '54',
'name' => 'Company 1',
'phone' => '867038198',
'start_date' => '2014-04-11'
)
),
(int) 1 => array(
'Company' => array(
'id' => '55',
'name' => 'Company 2',
'phone' => '867038199',
'start_date' => '2014-04-23'
)
))

然后我有以下 foreach 循环:

foreach($companies as $company){

$count = 3;
$company['Company']['count'] = $count;
}

当我尝试在 foreach 中调试 $company 时,一切正常并且计数已存储。但是当我返回整个数组 $companies 时,count 没有被存储。

非常感谢任何指导。

最佳答案

foreach 作用于数组的副本。您可以改用引用:

foreach($companies as &$company) {
$count = 3;
$company['Company']['count'] = $count;
}
unset($company); // clear the reference

关于php - 将数据插入foreach中的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23912530/

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