数组仅获取数据的第一部分

时间:2019-02-26 02:24:34

标签: php arrays associative-array

当您回显此特定代码以查看数据时,它仅显示数组的第一部分而不显示第二个索引,这是有原因的吗?

 $arrChartData = array(
  "seriesname" => "ACT",
  "data" => array (
      ["value" => "12000"],
      ["value" => "3000"],
      ["value" => "1000"]
  ),
  "seriesname" => "NSW",
  "data" => array (
      ["value" => "12000"],
      ["value" => "3000"],
      ["value" => "1000"]
  )
);

回显代码示例:

Array (
   [seriesname] => NSW
   [data] => Array (
       [0] => Array ( [value] => 12000 )
       [1] => Array ( [value] => 3000 )
       [2] => Array ( [value] => 1000 )
    )
)

1 个答案:

答案 0 :(得分:3)

数组必须具有唯一键,否则您将覆盖以前的值。在您的情况下,您可能需要嵌套数组:

$arrChartData = array(
    array(
      "seriesname" => "ACT",
      "data" => array (
          ["value" => "12000"],
          ["value" => "3000"],
          ["value" => "1000"]
      ),
    ),
    array(
      "seriesname" => "NSW",
      "data" => array (
          ["value" => "12000"],
          ["value" => "3000"],
          ["value" => "1000"]
      )
    )
);

var_export($arrChartData);的结果

array (
  0 => 
  array (
    'seriesname' => 'ACT',
    'data' => 
    array (
      0 => 
      array (
        'value' => '12000',
      ),
      1 => 
      array (
        'value' => '3000',
      ),
      2 => 
      array (
        'value' => '1000',
      ),
    ),
  ),
  1 => 
  array (
    'seriesname' => 'NSW',
    'data' => 
    array (
      0 => 
      array (
        'value' => '12000',
      ),
      1 => 
      array (
        'value' => '3000',
      ),
      2 => 
      array (
        'value' => '1000',
      ),
    ),
  ),
)

Demo