猿问

无法在 php 中创建具有相同项目名称的多个项目的数组

 $categoryArray=array();

 $dataseries1=array();

 $dataseries2=array();

 // pushing category array values

while($row = mysqli_fetch_array($result)) {   

 $lundra1 = $row['lundra'];

 $ghargoda1 = $row['ghargoda'];

 $shankargadh1 = $row['shankargarh'];

 $pratappur1 = $row['pratappur'];

 $charma1 = $row['charama'];

 $bharatpur1 = $row['bharatpur'];

 $carmdaksh1 = $row['carmdaksh'];

 $pali1 = $row['pali'];

 $magarload1 = $row['magarload'];

 $bodla1 = $row['bodla'];

 $chhindgarh1 = $row['chhindgarh'];

 $dantewada1 = $row['dantewada'];

 $bakawand1 = $row['bakawand'];

 $narharpur1 = $row['narharpur'];

 $total1 = $row['total'];    

 array_push($categoryArray, array(

          "label" => "Lundra",

          "label" => "Ghargoda",

          "label" => "Shankargarh",

          "label" => "Pratappur",

          "label" => "Charama"

          )

        );

这给出的输出为


    Array ( [0] => Array ( [label] => Charama ) )

我想做的是这样的


 Array ( [0] => Array ( [label] => Jan ) [1] => Array ( [label] => Feb ) [2] => Array ( [label] => Mar ) [3] => Array ( [label] => Apr ) [4] => Array ( [label] => May ) [5] => Array ( [label] => Jun ))

我是使用数组的新手,所以面临如何带来这个结果的问题


SMILET
浏览 113回答 2
2回答

料青山看我应如是

array_push($categoryArray, array(          "label" => "Lundra"          ),array(          "label" => "Ghargoda"          ),array(          "label" => "Shankargarh"          ),array(          "label" => "Pratappur"          ),array(          "label" => "Charama"          )        );这将为您提供所需的输出

翻翻过去那场雪

您面临的主要问题是数组是用键构建的。例如数字1 => First,2 => Second,...例如有名字first_entry => First,second_entry => Second,...当您在数组中定义多个条目时,它们会相互覆盖。要解决此问题,您必须在主数组中为每个项目定义一个数组。 array_push($categoryArray, array(      array("label" => "Lundra"),      array("label" => "Ghargoda"),      array("label" => "Shankargarh"),      array("label" => "Pratappur"),      array("label" => "Charama)"      )    );这样结果就会是这样的 Array ( [0] => Array ( [label] => Jan ) [1] => Array ( [label] => Feb ) [2] => Array ( [label] => Mar ) [3] => Array ( [label] => Apr ) [4] => Array ( [label] => May ) [5] => Array ( [label] => Jun ))。
随时随地看视频慕课网APP
我要回答