在循环中创建多维数组

时间:2022-08-06 21:28:51

I am trying to create an array like this in a loop:

我试图在循环中创建这样的数组:

$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));

with this code

用这个代码

$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']);
}

but when loop completes my array looks like this:

但是当循环完成时,我的数组看起来像这样:

Array ( [x] => 24 [y] => 819 ) 

4 个解决方案

#1


24  

This is because you're re-assigning $dataPoints as a new array on each loop.

这是因为您在每个循环上重新分配$ dataPoints作为新数组。

Change it to:

将其更改为:

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

This will append a new array to the end of $dataPoints

这将在$ dataPoints的末尾附加一个新数组

#2


2  

use array_merge($array1,$array2) make it simple use two array one for use in iteration and another for storing the final result. checkout the code.

使用array_merge($ array1,$ array2)可以简单地使用两个数组一个用于迭代,另一个用于存储最终结果。签出代码。

$dataPoints = array();  
$dataPoint = array();  

$brands = array(
    "COCACOLA","DellChannel","ebayfans","google","microsoft","nikeplus","amazon"); 
foreach($brands as $value){
    $resp = GetTwitter($value);
    $dataPoint = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);
    $dataPoints = array_merge($dataPoints,$dataPoint);
}

#3


1  

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

#4


0  

Every iteration you're overwriting $dataPoints variable, but you should add new elements to array...

每次迭代都要覆盖$ dataPoints变量,但是你应该向数组添加新的元素......

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

$ dataPoints [] = array(“x”=> $ resp ['friends_count'],“y”=> $ resp ['statuses_count']);

#1


24  

This is because you're re-assigning $dataPoints as a new array on each loop.

这是因为您在每个循环上重新分配$ dataPoints作为新数组。

Change it to:

将其更改为:

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

This will append a new array to the end of $dataPoints

这将在$ dataPoints的末尾附加一个新数组

#2


2  

use array_merge($array1,$array2) make it simple use two array one for use in iteration and another for storing the final result. checkout the code.

使用array_merge($ array1,$ array2)可以简单地使用两个数组一个用于迭代,另一个用于存储最终结果。签出代码。

$dataPoints = array();  
$dataPoint = array();  

$brands = array(
    "COCACOLA","DellChannel","ebayfans","google","microsoft","nikeplus","amazon"); 
foreach($brands as $value){
    $resp = GetTwitter($value);
    $dataPoint = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);
    $dataPoints = array_merge($dataPoints,$dataPoint);
}

#3


1  

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

#4


0  

Every iteration you're overwriting $dataPoints variable, but you should add new elements to array...

每次迭代都要覆盖$ dataPoints变量,但是你应该向数组添加新的元素......

$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);

$ dataPoints [] = array(“x”=> $ resp ['friends_count'],“y”=> $ resp ['statuses_count']);