我真的希望你帮助我解决这个问题,我希望这能为你提供帮助 - 我有这个foreach循环的伪示例:
foreach_loop {
$k1 = GetKey1();
$v1 = GetValue1();
$k2 = GetKey2();
$v2 = GetValue2();
$k3 = GetKey3();
$v2 = GetValue3();
// now I put those keys and values in associative array called DataArr
$DataArr[$k1] = $v1;
$DataArr[$k2] = $v2;
$DataArr[$k3] = $v3;
}
现在我的问题是,我如何创建一个数组,其中每个索引都包含一个从foreach
循环创建的关联数组,并像这样继续追加自己:
$resultArr = array(
0 => "DataArr_from_loop1",
1 => "DataArr_from_loop2",
2 => "DataArr_from_loop3",
3 => "DataArr_from_loop4"
//...etc
)
当我检查$resultArr[0]
时,我应该得到一个像这样的关联数组:
array (size=3)
'k1' => string 'v1'
'k2' => string 'v2'
'k3' => string 'v3'
我真的需要你的帮助,谢谢你。
答案 0 :(得分:0)
http://php.net/manual/en/function.array-push.php
int array_push ( array &$array , mixed $value1 [, mixed $... ] )
或
<?php
/**
* @desc array_push and removes elements from the beginning of the array until it is within limit
* @param array Array to push on to
* @param mixed Passed to array push as 2nd parameter
* @param int Limit (default = 10)
*
* @return array New array
*/
function array_push_limit($array,$add,$limit=10){
array_push($array, $add);
do {
array_shift($array);
$size=count($array);
} while($size > $limit);
return $array;
}
?>
----------
EXAMPLE:
----------
<?php
$array=array(1, -5, 23, -66, 33, 54, 3);
print_r(array_push_limit($array, "HELLO", 4));
?>
----------
OUTPUT:
----------
Array
(
[0] => 33
[1] => 54
[2] => 3
[3] => HELLO
)
答案 1 :(得分:0)
怎么样......
$resultArr = array();
foreach($whatever as $thing) {
$k1 = GetKey1();
$v1 = GetValue1();
$k2 = GetKey2();
$v2 = GetValue2();
$k3 = GetKey3();
$v2 = GetValue3();
// now I put those keys and values in associative array called DataArr
$DataArr = array();
$DataArr[$k1] = $v1;
$DataArr[$k2] = $v2;
$DataArr[$k3] = $v3;
$resultArr[] = $DataArr;
}