多维数组构造

时间:2018-09-12 18:56:44

标签: php multidimensional-array

我有从SQL结果中得到的这种类型的数组。

+-------------+---------------+-----------------+----------------+
| business_id | business_name | business_branch | branch_contact |
+-------------+---------------+-----------------+----------------+
|      1      |      ABC      |        1        |   1111111111   |
+-------------+---------------+-----------------+----------------+
|      1      |      ABC      |        2        |   2222222222   |
+-------------+---------------+-----------------+----------------+
|      1      |      ABC      |        3        |   3333333333   |
+-------------+---------------+-----------------+----------------+
|      1      |      ABC      |        4        |   4444444444   |
+-------------+---------------+-----------------+----------------+
|      2      |      XYZ      |        1        |   5555555555   |
+-------------+---------------+-----------------+----------------+
|      2      |      XYZ      |        2        |   6666666666   |
+-------------+---------------+-----------------+----------------+

在一开始,我想到了对每个分支机构和企业进行单独的数据库查询。但是要知道这是一个坏习惯。

我要按以下方式构造结果

[
{
  business_id : 1,
  business_name : ABC,
  branches : [
  {
    branch_id : 1,
    branch_contact : 1111111111
  },
  {
    branch_id : 2,
    branch_contact : 2222222222
   },
   ...
 },
 {
   business_id : 1,
   business_name : ABC,
   branches : [
   {
      ...
   }
 }
]
}

我尝试构建的当前函数未提供必要的输出。如下所示。

$previousBusiness = '';
$previousBranch = '';
$businessAndBranchArray = [];
$businessArray = [];
$branchArray = [];

foreach ($results as $result) {
    $currentBranch = [];
    $currentBusiness = [];
    $currentMerchant = [];

    if($result['business_id'] != $previousBusiness && $result['branch_id'] != $previousBranch){

        if($previousBranch != '' && $previousBusiness != ''){

            $businessArray['business_id'] = $result['business_id']; 
            $businessArray['business_id'] = $result['business_name'];

            $branchArray['branch_id'] = $result['branch_id'];
            $branchArray['branch_contact'] = $result['branch_contact'];

        } else {

            $businessArray['business_id'] = $result['business_id']; 
            $businessArray['business_id'] = $result['business_name'];

            $branchArray['branch_id'] = $result['branch_id'];
            $branchArray['branch_contact'] = $result['branch_contact'];
        }
        $previousBusiness = $result['business_id']; 

    } else {
        $branchArray['branch_id'] = $result['branch_id'];
        $branchArray['branch_contact'] = $result['branch_contact'];
    }

}

1 个答案:

答案 0 :(得分:1)

无需跟踪以前的项目,只需将ID用作数组键即可检查它是否存在。如果您不希望在最终输出中使用它,请使用array_values()删除键:

<?php
$results = [
    ["business_id" => 1, "business_name" => "ABC", "business_branch" => 1, "branch_contact" => "1111111111"],
    ["business_id" => 1, "business_name" => "ABC", "business_branch" => 2, "branch_contact" => "2222222222"],
    ["business_id" => 1, "business_name" => "ABC", "business_branch" => 3, "branch_contact" => "3333333333"],
    ["business_id" => 1, "business_name" => "ABC", "business_branch" => 4, "branch_contact" => "4444444444"],
    ["business_id" => 2, "business_name" => "XYZ", "business_branch" => 1, "branch_contact" => "5555555555"],
    ["business_id" => 2, "business_name" => "XYZ", "business_branch" => 2, "branch_contact" => "6666666666"],
];
$output = [];

foreach ($results as $result) {
    if (empty($output[$result["business_id"]])) {
        $output[$result["business_id"]] = [
            "business_id" => $result["business_id"],
            "business_name" => $result["business_name"],
            "branches" => [],
        ];
    }
    $output[$result["business_id"]]["branches"][] = [
        "branch_id" => $result["business_branch"],
        "branch_contact" => $result["branch_contact"],
    ];
}

echo json_encode(array_values($output), true);

输出:

[{"business_id":1,"business_name":"ABC","branches":[{"branch_id":1,"branch_contact":"1111111111"},{"branch_id":2,"branch_contact":"2222222222"},{"branch_id":3,"branch_contact":"3333333333"},{"branch_id":4,"branch_contact":"4444444444"}]},{"business_id":2,"business_name":"XYZ","branches":[{"branch_id":1,"branch_contact":"5555555555"},{"branch_id":2,"branch_contact":"6666666666"}]}]