我想将$secondArray
的值添加到$firstArray
:
$firstArray = [
0 => [
'prodID' => 101,
'enabled' => 1,
],
1 => [
'prodID' => 105,
'enabled' => 0,
],
];
$secondArray
将始终具有相同数量的数组项,并且与$firstArray
的顺序相同:
$secondArray = [34, 99];
这是我尝试过的,但在练习后我一直得到错误的stockQT
值:
foreach ($secondArray as $value) {
foreach ($firstArray as &$firstArray) {
$firstArray['stockQT'] = $value;
}
}
var_dump($firstArray)
:
array (size=2)
0 =>
array (size=3)
'prodID' => int 101
'subscribed' => int 1
'stockQT' => int 99
1 =>
array (size=3)
'prodID' => int 105
'subscribed' => int 0
'stockQT' => int 99
我查看了类似的帖子,但在使用while()
循环之类的不同建议后似乎无法获得正确的值。以下是我需要$firstArray
的样子:
array (size=2)
0 =>
array (size=3)
'prodID' => int 101
'subscribed' => int 1
'stockQT' => int 34
1 =>
array (size=3)
'prodID' => int 105
'subscribed' => int 0
'stockQT' => int 99
答案 0 :(得分:1)
您只需要一个using System;
public class Program
{
public static void Main()
{
int x = 0;
int a = 1000;
for(int i = 1; i <= a; i++)
{
x += i;
}
Console.WriteLine(x + " yours Sincerly, Allen");
}
}
使用密钥,因为foreach()
将始终具有相同数量的数组项,并且与$secondArray
的顺序相同。注意$firstArray
修改数组中的实际值:
&
或者循环foreach($firstArray as $key => &$value) {
$value['stockQT'] = $secondArray[$key];
}
并使用该键修改$secondArray
:
$firstArray