0

In this service I am getting output but I want different output. Let me explain:

    $customer_id = $_POST['customer_id'];
    $response = array();
    $qry="SELECT category FROM nesbaty_customer where c_id='".$customer_id."' ";
    $qry_res=mysqli_query($con,$qry);
    $jsonData = array();
    while ($array = mysqli_fetch_assoc($qry_res)) 
    {
        $r= $array['category'];

        $jsonData[]=explode(",",$r);

    }
    echo json_encode(array('data' =>$jsonData));
    mysqli_close($con);

I am getting Output like this:

{
"data": {
    "category": [
        "Hotel",
        "Saloon"
    ]
}

}

But I want output like this!

{"data":[{"category":"Hotel"},{"category":"Saloon"}]}

5 Answers5

2

You can't get exactly what you want because you would have an array with two identical keys. You can get something similar by changing:

$r= $array['category'];
$jsonData[]=explode(",",$r);

to

foreach (explode(',', $array['category']) as $cat) {
    $jsonData[]=array('category' => $cat);
}

$jsonData will look like this:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [category] => Hotel
                )
            [1] => Array
                (
                    [category] => Saloon
                )
        )
)

and the output from json_encode will be:

{"data":[{"category":"Hotel"},{"category":"Saloon"}]}
Nick
  • 138,499
  • 22
  • 57
  • 95
1

The keys should be unique, you shouldn't repeat "category" as a key.

"An object whose names are all unique is interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not unique, the behavior of software that receives such an object is unpredictable."

See https://www.rfc-editor.org/rfc/rfc7159 for JSON definition.

As you can see, json_encode uses this rfc: "PHP implements a superset of JSON as specified in the original » RFC 7159. "

If you really want to write non-unique keys, you should write your custom json encode function, but the decode function will have unpredictable behaviour.

Community
  • 1
  • 1
  • brother firstly understand my query! multiple data imploded in single row and want to explode all that data like this {"data":[{"category":"Hotel"},{"category":"Saloon"}]} – Jayvirsinh Vaghela May 10 '18 at 12:56
  • We are not speaking about the database key, but about the JavaScript Object keys. You are trying to use the same key "category" two times inside this object.data – IgrewupwithSlackware May 10 '18 at 13:16
0

Add each category as separate json object then:

while ($array = mysqli_fetch_assoc($qry_res)) {
    foreach ($array['category'] as $categoryName) {
        $jsonData[] = ['category' => $categoryName];
    }
}

This way you end up with an array of objects, thus you should be fine.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
-1

The key of the array must be unique

-2

while ($array = mysqli_fetch_assoc($qry_res)) {

    $jsonData[]=$array;

}
echo json_encode(array('data' =>$jsonData));
Hosain Ahmed
  • 115
  • 6