0

Short description: I query my PHP back-end which returns rows of data from an SQL database, the data is returned in JSON format. The data includes a link to an image for each row, to be able to create a placeholder I also determine the width and height of the image by reading 32KB of image data, using the "ranger" function as per webarto's answer here and then append this data to my resulting JSONArray.

This is the the code I have:

                    $result = $conn->query ( $sql );
                    $response = array ();
                    if ($result->num_rows > 0) {
                        while ( $row = $result->fetch_assoc () ) {
                            /* get image url */
                            $url = $row ['image'];
                            /* get the first 32KB of image data */
                            $raw = ranger ( $url );
                            /* recreate a part of the original image */
                            $im = imagecreatefromstring ( $raw );
                            /* determine width and height of the image */
                            $width = imagesx ( $im );
                            $height = imagesy ( $im );
                            /* add width and height at the end of the array */
                            array_push ( $row, $width );
                            array_push ( $row, $height );
                            $response [] = $row;
                        }
                        $j_result = json_encode ( $response );
                        echo $j_result;

Which gives the following result for one row:

 {"lang":"de","title":"Sample Title","article":"Sample text","time":"1416070535","image":"someimage.jpg","source":"sample.com","0":606,"1":379}

I'm not really familiar with PHP and managed to append the image dimensions using the array_push() function. However, this gives me "0", "1" etc. as the key for those values as you can see in the JSON above.

The question: how can I set those keys so json_encode does not set them automatically?

Community
  • 1
  • 1
Droidman
  • 11,485
  • 17
  • 93
  • 141
  • [Decode](http://php.net/manual/en/function.json-decode.php) the json into a PHP array, modify it then [encode](http://php.net/manual/en/function.json-encode.php) it again – Bankzilla Nov 23 '14 at 23:29

2 Answers2

2

json_encode isn't actually setting any indices of your JSON object, you are doing so in PHP. json_encode just takes the structure you provide it, and converts it to a string in valid JSON (if possible).

What you need to do is instead of using array_push($row, $width), do something along the lines of: $row['width'] = $width;, and the same for $height. Then your $row array will have the key width that points to the variable $width, and json_encode will encode it appropriately.

Rob
  • 1,865
  • 1
  • 14
  • 26
2

Instead of using array_push you can simply use the index you want directly:

$row['width'] = $width;
$row['height'] = $height;
DanielX2010
  • 1,897
  • 1
  • 24
  • 26