JSON_ENDODE why use to pass a PHP array? - php

I want to convert an array of objects to JSON encoding, but how it is possible to encode PHP array into JSON array.

if you want to convert php array into json
use:
json_encode($array);
json_encode
for decode
json_decode($array);
json_decode

Related

PHP json_encode encode only a key

As an API response to a payment gateway I receive an object, I would like it to become json so I used json_encode, but I only get the first key.
json_encode convert only the first key cause array is too large
json_encode convert array to json
json_decode convert json to array
API’s by default must give you a json and you must decode json data to array if you are going to use it on back-end.
Show some code, so we can help you

PHP display array in array in array

I want know how to access to array in array in array in php.
The result with $user is this:
'pages':{'x','y','z','access':{'a':3,'b':6,'c':8,'contact':2}}
How I can access to contact, please?
First of all this is JSON (probably), so before we can access it with php we need to decode it using json_decode which will give you a php object. (i made it valid JSON)
$jsonString = '{"pages":{"x": 0,"y": 0,"z": 0,"access":{"a":3,"b":6,"c":8,"contact":2}}}';
$phpObject = json_decode($jsonString);
var_dump($phpObject->pages->access->contact);
// prints int(2)
Use json_decode to convert this to array in PHP (if it is JSON)
It seems your string missing {} around.
{
"id":1,"active":1,"canAccess":{"entities":{"1":{"name":"blablabla","services":{"‌​45":{"name":"xxx"}}}}},"blabla":null,"pages":{"acces":{"notifications":1,"contact‌​":2}}
}
then it is a valid JSON string.
var_dump(json_decode($string,true));

convert POST array to json format

How can I convert php's POST variable to json format?
If this is entirely in php, and you simply want to convert the data in $_POST to JSON, you can do so like this:
$json = json_encode($_POST);
Using php's built-in function json_encode (doc)
If this is not what you want to do, please be more specific with your question.
json_encode($_POST);
Docs: http://php.net/manual/en/function.json-encode.php

Help editing JSON to make an array rather than a 'dictionary'

I currently have json using json_encode from a mysql query which looks like this:
{"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}, {"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}
How can I have the json being an array of posts ('post_2', 'post_1') rather than it being a dictionary? The JSON will be decoded on an iPhone using SBJSON and the JSON will have to be made into an array in the backend.
Thanks in advanced.
Provide a non-associative array to json_encode(). The easiest way is usually to simply call array_values() on the (associative) array, and encode the result.
Take a look at PHP's json_decode function, specifically the 2nd parameter if you want an array.

how to convert json to string

how do i convert a json object to a string
i want to insert a json object to a mysql DB
You might be interested in json_encode().
On the other hand if you already got something json encoded then it already is a string and you can simply store it as-is in the database.
JSON itself is a string.
People use json_encode() and json_decode() to convert objects/arrays of PHP to string and back to objects/arrays.
$array = json_decode($json,true);
$string = implode(",",$array);
Now you can save this string in db as text.

Categories