gpt4 book ai didi

php - array_combine 或 array_merge?

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

我敢肯定这对于精通 php 的人来说很容易,但我犯了一个错误,使我的大脑重载,所以现在我真的很困惑是否应该使用 array_combine、array_merge 或其他东西...我已经用谷歌搜索和阅读 php.net 4 个小时了,我想我只是让自己更加困惑...

本质上,我只是想在保留键的同时组合一个数组?

//Here are the original arrays
[field_sreference] => Array
(
[0] => Array
(
[nid] => 28
)

[1] => Array
(
[nid] => 28
)

[2] => Array
(
[nid] => 29
)
)

[field_idelta] => Array
(
[0] => Array
(
[value] => 0
)

[1] => Array
(
[value] => 1
)

[2] => Array
(
[value] => 0
)
)

[field_iswitch] => Array
(
[0] => Array
(
[value] => 0
)

[1] => Array
(
[value] => 0
)

[2] => Array
(
[value] => 0
)
)

//Here is what I'm trying to achieve:

[combinedarray] => Array
(
[0] => Array
(
[nid] => 28
[idelta] => 0
[iswitch] => 0

)

[1] => Array
(
[nid] => 28
[idelta] => 1
[iswitch] => 0
)

[2] => Array
(
[nid] => 29
[idelta] => 0
[iswitch] => 0
)
)

最佳答案

你可以通过简单地迭代数组来解决这个问题是 O(n)...

$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
$combinedarray[] = array("nid" => $field_sreference[$i]['nid'],
"idelta" => $filed_idelta[$i]['value'],
"iswitch" => $field_iswitch[$i]['value']);
}

这假设 3 个数组的长度都相等。

关于php - array_combine 或 array_merge?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10180271/

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