我正在尝试在循环中创建这样的数组:
$dataPoints = array(
array('x' => 4321, 'y' => 2364),
array('x' => 3452, 'y' => 4566),
array('x' => 1245, 'y' => 3452),
array('x' => 700, 'y' => 900),
array('x' => 900, 'y' => 700));
使用此代码
$dataPoints = array();
$brands = array("COCACOLA","DellChannel","ebayfans","google",
"microsoft","nikeplus","amazon");
foreach ($brands as $value) {
$resp = GetTwitter($value);
$dataPoints = array(
"x"=>$resp['friends_count'],
"y"=>$resp['statuses_count']);
}
但是当循环完成时,我的数组看起来像这样:
Array ( [x] => 24 [y] => 819 )
最佳答案
这是因为您在每个循环上将 $dataPoints
重新分配为新数组。
将其更改为:
$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);
这会将一个新数组附加到 $dataPoints
的末尾
关于php - 在循环中创建多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4497149/