为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
SMILET 2023-10-15 15:13:51
 $categoryArray=array(); $dataseries1=array(); $dataseries2=array(); // pushing category array valueswhile($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 ))我是使用数组的新手,所以面临如何带来这个结果的问题
查看完整描述

2 回答

?
料青山看我应如是

TA贡献1772条经验 获得超8个赞

array_push($categoryArray, array(

          "label" => "Lundra"

          ),array(

          "label" => "Ghargoda"

          ),array(

          "label" => "Shankargarh"

          ),array(

          "label" => "Pratappur"

          ),array(

          "label" => "Charama"

          )

        );

这将为您提供所需的输出


查看完整回答
反对 回复 2023-10-15
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

您面临的主要问题是数组是用键构建的。


例如数字


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 ))。


查看完整回答
反对 回复 2023-10-15
  • 2 回答
  • 0 关注
  • 103 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信