Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kenprogrammer's avatar

PHP Format Key of the JSON Object

Given the following code snippet:

<?php
    require "db_conn.php";

    $sql = "SELECT category,product_code,product_name,unit,quantity FROM items INNER JOIN categories ON items.category_id=categories.id";

    $res = $conn->query($sql)->fetchAll();

    $structured = [];

    if ($res) {

         foreach ($res as $key => $row)
         {
            $structured[$row['category']]['items'][] = [
               'product_code' => $row['product_code'],
               'product_name' => $row['product_name'],
               'unit' => $row['unit'],
               'quantity' => $row['quantity'],
            ];

         }

         echo json_encode([$structured]);
    }

The JSON response for the above code is as follows:

[{
	"BABY ITEMS": {
		"items": [{
			"product_code": "151128",
			"product_name": "BOUNCY BABY WIPES 80'S",
			"unit": "CARTON",
			"quantity": "5.00"
		}]
	},
	"CONFECTIONS\/PASTRIES": {
		"items": [{
			"product_code": "130570",
			"product_name": "NUVITA FAMILY 75G",
			"unit": "CARTON",
			"quantity": "1.00"
		}, {
			"product_code": "115165",
			"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
			"unit": "CARTON",
			"quantity": "2.00"
		}]
	},
	"HOUSEHOLD ITEMS": {
		"items": [{
			"product_code": "150278",
			"product_name": "BOUNCY BABY DIAPER 10'S MINI",
			"unit": "CARTON",
			"quantity": "1.00"
		}]
	}
}]

How do I modify this part of code:

$structured[$row['category']]['items'][]

to output:

"category": "BABY ITEMS",

instead of:

"BABY ITEMS": 

The expected JSON is (Handwritten):

[{
		"category": "BABY ITEMS",
		"items": [{
			"product_code": "151128",
			"product_name": "BOUNCY BABY WIPES 80'S",
			"unit": "CARTON",
			"quantity": "5.00"
		}]
	},
	{
		"category": "CONFECTIONS/PASTRIES",
		"items": [{
				"product_code": "130570",
				"product_name": "NUVITA FAMILY 75G",
				"unit": "CARTON",
				"quantity": "1.00"
			},
			{
				"product_code": "115165",
				"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
				"unit": "CARTON",
				"quantity": "3.00"
			},
			{
				"product_code": "115160",
				"product_name": "NUVITA MAGIK S.BERRY CRM 60*2'S",
				"unit": "CARTON",
				"quantity": "2.00"
			}
		]
	},
	{
		"category": "HOUSEHOLD ITEMS",
		"items": [{
			"product_code": "150278",
			"product_name": "BOUNCY BABY DIAPER 10'S MINI",
			"unit": "CARTON",
			"quantity": "1.00"
		}]
	},
	{
		"category": "PERSONAL CARE",
		"items": [{
			"product_code": "160200",
			"product_name": "ALL TYME ULTRA REGULAR 8'S",
			"unit": "CARTON",
			"quantity": "2.00"
		},
		{
			"product_code": "160309",
			"product_name": "ALL TYME ULTRA MEDIUM 8'S",
			"unit": "CARTON",
			"quantity": "4.00"
		},
		{
			"product_code": "160235",
			"product_name": "GOLDEN SHOE POLSIH 50ML BLACK",
			"unit": "CARTON",
			"quantity": "1.00"
		},
		{
			"product_code": "190251",
			"product_name": "ALL TYME ULTRA MEDIUM 16'S",
			"unit": "CARTON",
			"quantity": "2.00"
		}]
	}
]
0 likes
3 replies
Dunsti's avatar

what about this:

$structured[
	'category' => $row['category'],
	'items' => [
               'product_code' => $row['product_code'],
               'product_name' => $row['product_name'],
               'unit' => $row['unit'],
               'quantity' => $row['quantity'],
            ]
];
kenprogrammer's avatar

@Dunsti I had tried this and got the error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']'

Please or to participate in this conversation.