Multi Dimensional Array Construct

Tharindu

I have this type of array that I got from my SQL result.

+-------------+---------------+-----------------+----------------+
| 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   |
+-------------+---------------+-----------------+----------------+

At the beginning I thought of doing a separate database query for each branches and businesses. But got to know it was a bad practice.

I want to construct the results as following

[
{
  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 : [
   {
      ...
   }
 }
]
}

The current function I'm trying to build doesn't give the necessary output. It is shown below.

$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'];
    }

}
miken32

No need to keep track of previous items and such, just use the ID as an array key so you can check it exists. If you don't want it in your final output, use array_values() to remove the keys:

<?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);

Output:

[{"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"}]}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related