结合循环数组PHP Codeigniter

时间:2017-11-15 13:24:48

标签: php arrays json codeigniter

我有两个数组是两个不同数据库查询的结果。我想按照我在下面的例子中解释的方式合并这两个数组

这是执行第一个查询的代码:

$query = $this->db->query("SELECT kec,pilihan,COUNT(pilihan) as total FROM votes GROUP BY pilihan");
$someArray = [];
foreach($query->result_array() as $row){
    array_push($someArray, [
        'data'     => $row['pilihan'],
        'total'    => $row['total'],
        'location' => $row['kec'],
        ]);
    }

这是结果数组:

[
  {
    "data": "1",
    "total": "2",
    "location": "3524230"
  },
  {
    "data": "2",
    "total": "3",
    "location": "3524230"
  },
  {
    "data": "3",
    "total": "1",
    "location": "3524230"
  }
]

这是执行第二个查询的代码:

$query = $this->db->query("SELECT * FROM owl_dptmaster GROUP BY kecamatan");
$someArray = [];
foreach($query->result_array() as $row){
    array_push($someArray, [
        'location' => $row['kecamatan']
    ]);
}

第二个数组看起来像这样:

[
  {
    "location": "3524230"
  },
  {
    "location": "3524240"
  },
  {
    "location": "3524250"
  },
  {
    "location": "3524260"
  }
]

我想合并这两个数组,以便结果就是这个数组:

[
  {
    "location": "3524230",
    "data1": "2",
    "data2": "3",
    "data3": "1"
  },
  {
    "location": "3524240",
    "data1": null,
    "data2": null,
    "data3": null
  },
  {
    "location": "3524250",
    "data1": null,
    "data2": null,
    "data3": null
  },
  {
    "location": "3524260",
    "data1": null,
    "data2": null,
    "data3": null
  }
]

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用子查询在sql中进行汇总,而不是组合数组。假设你正在使用mysql。

$query = $this->db->query(
 "select owl.kecamatan,
  (select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 1) data1,
  (select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 2) data2,
  (select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 3) data3,
 from owl_dptmaster owl
 order by owl.kecamatan"
 );

您可以执行此查询,然后构建一个"data" . $pilihan => $totalPilihan数组,当$someArray发生更改时,您可以将其推送到$location

$query = $this->db->query(
"select owl.kecamatan as location, v.pilihan
from owl_dptmaster owl
left join votes v
on owl.kecamatan = v.kec"
order by 1, 2);

您拥有包含所有地点和投票的结果。

 // Made of query results...
 $query = [

 [ "location" => "3524230", "pilihan" => "1" ],
 [ "location" => "3524230", "pilihan" => "1" ],
 [ "location" => "3524230", "pilihan" => "2" ],
 [ "location" => "3524230", "pilihan" => "3" ],
 [ "location" => "3524230", "pilihan" => "3" ],
 [ "location" => "3524230", "pilihan" => "3" ],
 [ "location" => "3524230", "pilihan" => "4" ],
 [ "location" => "3524230", "pilihan" => "4" ],
 [ "location" => "3524230", "pilihan" => "5" ],
 [ "location" => "3524240", "pilihan" => null ],
 [ "location" => "3524250", "pilihan" => null ],
 [ "location" => "3524260", "pilihan" => null ],
 [ "location" => "3524270", "pilihan" => "1" ],
 [ "location" => "3524270", "pilihan" => "1" ],
 [ "location" => "3524270", "pilihan" => "1" ],
 [ "location" => "3524270", "pilihan" => "20" ],
 [ "location" => "3524270", "pilihan" => "20" ],
 [ "location" => "3524270", "pilihan" => "30" ]

 ];

这是构建$someArray[]的程序例程。

 $someArray = [];
 $xRow = [];
 $oldPilihan = '';
 $total = 0;
 $oldLocation = '';

 foreach ($query as $row) {

 //echo $row['location'] . " - " . $row['pilihan'] . '<br>';

 $location = $row['location'];
 $pilihan = $row['pilihan'];

 if ($pilihan !== $oldPilihan) {

 if (! empty($oldPilihan) ) {
   $dataName = 'data' . $oldPilihan;
   $xRow[] = array($dataName => $total);
 }
   $total = 1;
   $oldPilihan = $pilihan;

 } else {

   $total++;
 }

 if ($location !== $oldLocation) {

   if (! empty($oldLocation)) {
     array_push($someArray, $xRow);
   }
   $xRow = [ 'location' => $location ];
   $oldLocation = $location;

   }

 }

 $dataName = 'data' . $oldPilihan;
 $xRow[] = array($dataName => $total);
 array_push($someArray, $xRow);

 var_dump($someArray);