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

4 Answers 4

2

Try

echo json_encode((object)$jsonobj);
Sign up to request clarification or add additional context in comments.

4 Comments

thanks that did the trick in my case! however any idea on how to setting a number as key for an associative array?
uhm... I don't understand. It seems you are doing exactly that.
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")
It seems it is not possible. See this question.
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"
    }
}

Comments

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.

Comments

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

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.