Php数组从数据库没有得到所需的数组结构



我有问题,通过从数据库中提取数据构建多维数组。我的数据库中有一份销售代表(sr)的名单。我运行查询并选择所有sr。现在我要创建这样的数组:

array (size=3)
  'Manoj' =>
    array (size=3)
      'mc_count' => 0
      'auto_count' => 0
      'in_count' => 0
  'Bharat' =>
    array (size=3)
      'mc_count' => 0
      'auto_count' => 0
      'in_count' => 0
  'Pradeep' =>
    array (size=3)
      'mc_count' => 0
      'auto_count' => 0
      'in_count' => 0

我写了下面的代码:

<?php
$sr_array=array();
$sr_sql= "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
       $sr_array []= array($sr_result["sr"]=>array(“mc_count”,”auto_count”,”in_coun”);
        }
var_dump($sr_array);
?>

我得到这个OUTPUT

array (size=9)
  0 =>
    array (size=1)
      'Manoj' =>
        array (size=3)
          'count_in_battery' => int 10
          'count_auto_battety' => int 0
          'count_indu_battery' => int 0
  1 =>
    array (size=1)
      'Bharat' =>
        array (size=3)
          'count_in_battery' => int 10
          'count_auto_battety' => int 0
          'count_indu_battery' => int 0
  2 =>
    array (size=1)
      'Pradeep =>
        array (size=3)
          'count_in_battery' => int 10
          'count_auto_battety' => int 0
          'count_indu_battery' => int 0

如果你看到输出,我的数组有额外的索引

0 =>   //Unwanted Index
    array (size=1)
      'Manoj' =>

这会在编程中产生问题。

试试这个:

$sr_array = array();
$sr_sql = "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
    $sr_array[$sr_result["sr"]] = array(
        "mc_count" => 0,
        "auto_count" => 0,
        "in_coun" => 0
    );
}

相关内容

  • 没有找到相关文章

最新更新