2

I am fetching data from DB like this

$endResult = array();

  while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
    if (!isset($endResult[$row['car']])) {
            $endResult[$row['car']]= (object) array(
              'car' => $row['car'],
              'carModel' => $row['carModel'],
              'colors' => array()
        );
    }

                $endResult[$row['car']] -> colors [] = (object) array(
                    'paintedOn' => $row['paintenOnDate'],
                    'paintedBy' => $row['paintedBy']
                );
            }

//return with slim.php 
$response->body(json_encode($endResult));

and result I am getting

{"1": 
  {
    "car": "1",
    "carModel": "model-1",
    "colors": [
        {
            "paintedOn": "2014-11-07",
            "paintedBy": "5"
        },{
            "paintedOn": "2014-11-08",
            "paintedBy": "6"
        }]
     },
"2":{
    "car": "2",
    "carModel": "model-2",
    "colors": [
        {
            "paintedOn": "2014-11-09",
            "paintedBy": "7"
        },{
            "paintedOn": "2014-11-10",
            "paintedBy": "8"
        }]
     }
   }//<--replace this with []

Even if $endResult is declared as Array I am getting {} brackets, how could I replace "Object" brackets with "Array" brackets?

UPDATE: I can't remove json_encode as the front-end (backbone) expecting collection

UPDATE 2: $endResult = array(); return [...] but this $endResult[$row['car']]= (object) array(...) convert it to {...}

3
  • You encode it to json. Json works with {}. Commented Nov 11, 2014 at 12:48
  • but if I do like this $stmt->fetchAll(PDO::FETCH_OBJ); it will return it as [...] Commented Nov 11, 2014 at 12:49
  • You try json_decode() ?? Commented Nov 11, 2014 at 12:50

4 Answers 4

2

You can't achieve what you want because it would result in invalid JSON. According to json.org:

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

So you can only have values in an array. Because you are adding a name to the value, it must become an object.

If you really want your JSON to be wrapped in an array you need to remove the first-level names, in your example "1" and "2":

[
    {
        "car": "1",
        "carModel": "model-1",
        "colors": [
            {
                "paintedOn": "2014-11-07",
                "paintedBy": "5"
            },
            {
                "paintedOn": "2014-11-08",
                "paintedBy": "6"
            }
        ]
    },
    {
        "car": "2",
        "carModel": "model-2",
        "colors": [
            {
                "paintedOn": "2014-11-09",
                "paintedBy": "7"
            },
            {
                "paintedOn": "2014-11-10",
                "paintedBy": "8"
            }
        ]
    }
]
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the conversion to JSON. Also "declaration" in PHP doesn't matter. You can still assign different types in the course of your program.

Comments

0

I think the PHP function json_decode Should help you here. This turns the JSON format into an array.

Comments

0
use json_decode($data,true) for convert returned  data in to array.

Comments

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.