Vahid Alvandi Vahid Alvandi - 18 days ago 5
JSON Question

how i can convert php array to json format

hello for example i have bellow json format. i need generate a php array to build this json from that

{
"posts":
[
{
"detial" : {
"titel": "web design",
"date-publish" : "2015-02-08",
"pic-thumbnail" : "small-web-desing-jpg",
"pic-medium" : "medium-web-desing-jpg"
},
"content" : {
"description" : "description web design post",
"content" : "web design is modern technology in world"
}
}
]
}


please show An example of that PHP Array build this json

Answer

It is Easy !

Simply use PHP's json_decode() function to acheive that.

Finally, save your json_encode() return array to a variable.

Output:

Array
(
    [posts] => Array
        (
            [0] => Array
                (
                    [detial] => Array
                        (
                            [titel] => web design
                            [date-publish] => 2015-02-08
                            [pic-thumbnail] => small-web-desing-jpg
                            [pic-medium] => medium-web-desing-jpg
                        )

                    [content] => Array
                        (
                            [description] => description web design post
                            [content] => web design is modern technology in world
                        )

                )

        )

)

PHP Code:

$results = json_decode('{

  "posts": [{

    "detial" : {

      "titel": "web design",
        "date-publish" : "2015-02-08",
        "pic-thumbnail" : "small-web-desing-jpg",
        "pic-medium" : "medium-web-desing-jpg"

    },

    "content" : {

      "description" : "description web design post",
      "content" : "web design is modern technology in world"

    }

  }]

}', true);

echo "<pre>".print_r($results, true)."</pre>";
Comments