Ashish Kudale
Ashish Kudale

Reputation: 1260

add array in each object of array in php

I am new to PHP. I wanted to add array in each object of array. It is array inside array and each array object contains a array. I also search on internet I didn't find anything related to this.

Here is my desire json

[{  
      "id":1,
      "name":"Grey",
      "list":[  
         {  
            "id":1,
            "name":"60 X 60 ABC"
         },
         {  
            "id":2,
            "name":"40 X 40 PQR"
         },
         {  
            "id":3,
            "name":"45 X 45 XYZ"
         }
      ]
   },
   {  
      "id":2,
      "name":"Yarn",
      "list":[  
         {  
            "id":4,
            "name":"YARN ABC"
         },
         {  
            "id":5,
            "name":"YARN XYZ"
         }
      ]
   }]

All this data is filled from mysql database.

Here is my php code

$sql = "select * from tblType";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($rows = $result->fetch_assoc()) {

        $obj = new stdClass;

        $obj->id = $rows["id"];
        $obj->name = $rows["name"];
        $obj->list = $array2;

        $sql2 = "select * from tblQuality where typeId = $obj->id";
            $result2 = $conn->query($sql2);

            if ($result2->num_rows > 0) {
                while($row = $result2->fetch_assoc()) {
                    $obj2 = new stdClass;

                    $obj2->id = $row["id"];
                    $obj2->name = $row["name"];

                    array_push($array2, $obj2);
                }
            }
        array_push($array, $obj);
    }
}

echo json_encode($array);

mysqli_close($conn);

I don't know how to add array inside array. Please help.

Upvotes: 1

Views: 70

Answers (2)

motanelu
motanelu

Reputation: 4025

Here you are:

$sql = "select * from tblType";
$result = $conn->query($sql);

$results = [];

if ($result->num_rows > 0) {
    while($rows = $result->fetch_assoc()) {
        $item = [];
        $item['id'] = $rows['id']
        $item['name'] = $rows['name'];
        $item['list'] = [];

        $sql2 = "select * from tblQuality where typeId = " . $rows['id'];
        $result2 = $conn->query($sql2);

        if ($result2->num_rows > 0) {
            while($row = $result2->fetch_assoc()) {
                $item['list'][] = [
                    'id' => $row['id'],
                    'name' => $row['name']
                ]
            }
        }

        $results[] = $item
    }
}

echo json_encode($results);

mysqli_close($conn);

Hint: don't use stdClass objects as arrays. They're not meant for that.

Upvotes: 1

Danilo Bustos
Danilo Bustos

Reputation: 1093

try this:

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$sql = "select * from tblType";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($rows = $result->fetch_assoc()) {

        $obj = new stdClass;

        $obj->id = $rows["id"];
        $obj->name = $rows["name"];
        $obj->list = array();

        $sql2 = "select * from tblQuality where typeId = $obj->id";
            $result2 = $conn->query($sql2);

            if ($result2->num_rows > 0) {
                while($row = $result2->fetch_assoc()) {
                    $obj2 = new stdClass;

                    $obj2->id = $row["id"];
                    $obj2->name = $row["name"];

                    array_push( $obj->list, $obj2);
                }
            }
        array_push($array, $obj);
    }
}

echo json_encode($array);

mysqli_close($conn);

Upvotes: 1

Related Questions