0

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

1

1 Answer 1

1

use category name as index

while ($row = mysqli_fetch_array($result)) {
    $data[$row['category_name']] = array(
        'image' => $row['file_s']
    );
}
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

2 Comments

didn't get you? Please describe a bit more.
If I use $data[$row['category_name']] so what am I pass in json_encode function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.