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"}]}
8
  • As you wrote it down, it has duplicated key category, so you cant get it Commented May 10, 2018 at 7:42
  • category is my db table field name Commented May 10, 2018 at 7:45
  • Here it is key for two elements of one same array Commented May 10, 2018 at 7:49
  • stackoverflow.com/questions/4844223/… Commented May 10, 2018 at 8:12
  • Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. Commented May 10, 2018 at 8:30

5 Answers 5

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"}]}
Sign up to request clarification or add additional context in comments.

8 Comments

{ "data": { "category": [ "Hotel", "Saloon" ] } }
this is my output
BUT i want This {"data":[{"category":"Hotel"},{"category":"Saloon"}]}
But that is the output of this code... are you sure you copied it correctly?
again tell me above two line will replace by your code?
|
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.

2 Comments

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"}]}
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
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.

Comments

-1

The key of the array must be unique

Comments

-2

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

    $jsonData[]=$array;

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

1 Comment

oh men ! i want to explode two category it will return in one!!

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.