php将数组值循环并将最终值推送到数组中

时间:2016-04-01 04:47:16

标签: php json while-loop

我有一段代码需要从存储过程中获取值,使用循环必须对结果值求和,最后推入数组生成json,

我的代码,

<?php
  $fromdate="2016-03-31";
  $todate="2016-03-31";
  $TAG="1";

  $con = mysqli_connect("XXXXX","XXXX","XXX","XXXX");

   $query = mysqli_query($con, "CALL sp_Android_Online_Dashboard('$fromdate', '$todate','$TAG')") or die("Query fail: " . mysqli_error());

 $response["Details"] = array();    
 while(($row = mysqli_fetch_array($query))) 
 {
 $value = array();
 $tot_files= $row["Totfiles"];
 $value["Totfiles"] +=$tot_files; //final sum value of "Totfiles"

 $tot_file_mins= $row["file_minutes"];
 $value["file_minutes"] +=$tot_file_mins; //final sum value of "file_minutes"
 array_push($response["Details"], $value); 
 }

  echo json_encode($response);       
 ?>

输出现在的内容,

   {  
   "Details":[  
      {  
         "Totfiles":55,
         "file_minutes":125
      },
      {  
         "Totfiles":60,
         "file_minutes":82
      },
      {  
         "Totfiles":7,
         "file_minutes":75
      },
      {  
         "Totfiles":8,
         "file_minutes":108
      },
      {  
         "Totfiles":9,
         "file_minutes":12
      },
      {  
         "Totfiles":7,
         "file_minutes":11
      },
      {  
         "Totfiles":13,
         "file_minutes":30
      },
      {  
         "Totfiles":12,
         "file_minutes":28
      },
      {  
         "Totfiles":8,
         "file_minutes":15
      },
      {  
         "Totfiles":3,
         "file_minutes":4
      },
      {  
         "Totfiles":4,
         "file_minutes":6
      },
      {  
         "Totfiles":1,
         "file_minutes":1
      },
      {  
         "Totfiles":3,
         "file_minutes":6
      },
      {  
         "Totfiles":2,
         "file_minutes":4
      },
      {  
         "Totfiles":64,
         "file_minutes":339
      },
      {  
         "Totfiles":24,
         "file_minutes":96
      },
      {  
         "Totfiles":3,
         "file_minutes":6
      },
      {  
         "Totfiles":19,
         "file_minutes":48
      },
      {  
         "Totfiles":2,
         "file_minutes":10
      },
      {  
         "Totfiles":1,
         "file_minutes":2
      },
      {  
         "Totfiles":7,
         "file_minutes":32
      },
      {  
         "Totfiles":4,
         "file_minutes":16
      }
   ]
} 

我想要的输出,

{"Details":[{"Totfiles":695,"file_minutes":365}]}

提前致谢

2 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$fromdate="2016-03-31";
  $todate="2016-03-31";
  $TAG="1";

  $con = mysqli_connect("XXXXX","XXXX","XXX","XXXX");

   $query = mysqli_query($con, "CALL sp_Android_Online_Dashboard('$fromdate', '$todate','$TAG')") or die("Query fail: " . mysqli_error());

 $response["Details"] = array();   


$Totfiles = 0;
$file_minutes = 0;
while(($row = mysqli_fetch_array($query))) 
 {
 $Totfiles +=$row["Totfiles"]; //final sum value of "Totfiles"


 $file_minutes +=$row["file_minutes"]; //final sum value of "file_minutes"

 }

$response["Details"]['Totfiles'] = $Totfiles;
$response["Details"]['file_minutes'] = $file_minutes;

echo json_encode($response);      
?>

我没有测试过这段代码,但希望这可行。

答案 1 :(得分:1)

只需在循环内添加值,然后在完成循环后将值推送到数组。

$totalfile=0;
$totalmin=0;
while(($row = mysqli_fetch_array($query))) 
     $value = array();
     $tot_files= $row["Totfiles"];
     $totalfile +=$tot_files; //final sum value of "Totfiles"
     $tot_file_mins= $row["file_minutes"];
     $totalmin +=$tot_file_mins; //final sum value of "file_minutes"

 }
$response["Details"]["Totfiles"]=$totalfile;
$response["Details"]["file_minutes"]=$totalmin;
 echo json_encode($response);