3

I'm trying to recreate json from a DB, for the client side. Unfortunately some of the keys in the json are numbers, which work fine in javascript, however as a result PHP keeps treating them as numeric instead of associative arrays. each key is for a document. let me show you:

PHP:

  $jsonobj;
  while ($row = mysql_fetch_assoc($ms)) {

            $key = strval($row["localcardid"]);
            $jsonobj[$key] = json_decode($row["json"]);
    }
    // $jsonobj ist still a numeric array
    echo json_encode($jsonobj);

the resulting json should look like this:

{
  "0": {
    "terd": "10",
    "id": 0,
    "text": "",
    "pos": 1,
    "type": 0,
    "divs": [
        {},
        {}
    ],
    "front": 1
 }
"1": {
     "terd": "10",
    "id": 0,
    "text": "",
    "pos": 1,
    "type": 0,
    "divs": [
        {},
        {}
    ],
    "front": 1
  }
}

One obvious solution would be to save the whole json without splitting in up. however that doesnt seem wise in regards to the db. i wanna be able to access each document seperately. using

 $jsonobj = array ($key => json_decode($row["json"]));

obviously works, but unfortunately just for one key...

EDIT: for clarification* in php: there's a difference between

array("a", "b", "c")      

and

   array ("1" => "a", "2" => "b", "3" => "c").

the latter, when done like this $array["1"] = "a" results in array("a") instead of array("1" => "a")

ANSWERED HERE

Community
  • 1
  • 1
jcfrei
  • 1,819
  • 4
  • 19
  • 35

4 Answers4

2

Try

echo json_encode((object)$jsonobj);
Vatev
  • 7,493
  • 1
  • 32
  • 39
  • thanks that did the trick in my case! however any idea on how to setting a number as key for an associative array? – jcfrei Jul 26 '12 at 22:23
  • uhm... I don't understand. It seems you are doing exactly that. – Vatev Jul 26 '12 at 22:26
  • in php: there's a difference between `array("a", "b", "c")` and `array ("1" => "a", "2" => "b", "3" => "c")`. the latter, when done like this `$array["1"] = "a"` results in `array("a")` instead of `array("1" => "a")` – jcfrei Jul 26 '12 at 22:32
  • 1
    It seems it is not possible. See [this question](http://stackoverflow.com/questions/4100488/a-numeric-string-as-array-key-in-php). – Vatev Jul 26 '12 at 22:34
2

I believe if you pass the JSON_FORCE_OBJECT option, it should output the object with numeric indexes like you want:

$obj = json_encode($jsonObj, JSON_FORCE_OBJECT);

Example:

$array = array();

$array[0] = array('test' => 'yes', 'div' => 'first', 'span' => 'no');
$array[1] = array('test' => 'no', 'div' => 'second', 'span' => 'no');
$array[2] = array('test' => 'maybe', 'div' => 'third', 'span' => 'yes');

$obj = json_encode($array, JSON_FORCE_OBJECT);
echo $obj;

Output:

{
    "0": {
        "test": "yes",
        "div": "first",
        "span": "no"
    },
    "1": {
        "test": "no",
        "div": "second",
        "span": "no"
    },
    "2": {
        "test": "maybe",
        "div": "third",
        "span": "yes"
    }
}
drew010
  • 68,777
  • 11
  • 134
  • 162
0

Simply save both inside a single entry in the database: the separate field values AND the whole json structure inside a separate column. This way you can search by single fields and still get the valid json structure for easy handling.

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

For setting a number as key in associative array we can use following code

$arr=array(); //declare array variable
$arr[121]='Item1';//assign Value
$arr[457]='Item2';
.
.
.
print_r($arr);//print value
Haresh Kumar
  • 539
  • 6
  • 15