Reputation: 982
code:
<?php
session_start();
error_reporting(0);
include("config.php");
$sql = "select * from category";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
$data[] = array(
'category' => $row['category_name'],
'image' => $row['file_s']
);
}
echo json_encode($data);
?>
In this code I have convert mysql data in json format using json_encode
function. It display look like as below:
[
{
"category": "cap",
"image": "Cap-01.png"
},
{
"category": "hair",
"image": "Hair Color-01.png"
}
]
But I want it another way like:
[
cap {
"image": "Cap-01.png"
},
hair {
"image": "Hair Color-01.png"
}
]
So how can I do this? Please help me.
Thank You
Upvotes: 0
Views: 36
Reputation: 5501
use category name as index
while ($row = mysqli_fetch_array($result)) {
$data[$row['category_name']] = array(
'image' => $row['file_s']
);
}
echo json_encode($data);
Upvotes: 1