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.
Related
I am confused on what kind of data is this. Can anyone help me on this. I am trying to make it in readable format using PHP.
a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:6:"Sujith";s:10:"your-email";s:21:"sudhamenon990#gmail.com";s:12:"your-subject";s:5:"Hello";s:12:"your-message";s:16:"How are you man?";s:16:"ywctm-product-id";s:4:"1840";}
Thank you in advance.
It is a serialized array. You need to unserialize it
unserialize ( string $data , array $options = [] )
unserialize() takes a single serialized variable and converts it back into a PHP value.So that you can access it using php.
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
I have an array with multiple items in it one of which is an already encoded json string. I'm wanting to json encode the whole array but in doing so it re-json_encodes the json and adds slashes to it. The only way I've found to fix this is to json_decode the value and then encode the whole array. I feel like this is a waste of resources though and I feel like there has to be a better way. Is doing it that way the best possible way?
Here's a sample of the array I'm trying to json_encode.
$arr = array();
$arr["var1"] = '{"test":"test"}';
$arr["var2"] = 'foo';
$arr["var3"] = 'bar';
If I don't decode the var1 first and I just encode the whole array I get a result like so
{"var1":"{\"test\":\"test\"}","var2":"foo","var3":"bar"}
Notice the slashes in the json object.
json_encode() returns a string containing the json representation of a value.
In the example, a php string is passed as one element of the array '{"test":"test"}', thus json_encode() is encoding it appropriately into json format, with escaped quotes "{\"test\":\"test\"}".
If decoding nested json is a very resource heavy task, a workaround is to use a placeholder instead of the value, {"var1":"PLACEHOLDER","var2":"foo","var3":"bar"}, and then using str_replace() to replace it.
However, simply decoding it as you described is probably a cleaner solution, if its not resource heavy.
I want to save a long array in mysql database and when i read that array back from mysql database, i want that array back in proper array format. Is this possible?
Other threads are suggesting to convert array into string using serialise and explode. But will they help me to get proper array back?
Thanks.
You can try it yourself.
As an alternative to serailize(), you can use json_encode().
sidenote: reverse of serialize() is unserialize(), not explode().
sidenote: reverse of json_encode() is json_decode().
sidenote: Very worthwhile to read: Discussion of json_encode() vs serialize()
Usually, serialize and unserialize functions are used for such purposes.
With serialize you can convert your array to a string and than get an array by applying unserialize on string you get from serialize.
And about explode: you may use it too, but than you will need to use implode function to serialize array. But it will work with simplest one dimensional arrays only:
implode(",", array("val1", "val2", "val3")) = "val1,val2,val3"
Hi this is the code i am following in my project.
$reports = $this->curl->simple_get('url');
echo is_array($reports)?"Is Array":"Not Array";exit;
It's giving Not Array as output.
I want to convert that into associative array.
The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.
To use this you can use a similar solution as brought out here:
Create variable from print_r output
it describes the print_r_reverse function that's brought out in php.net page.
how ever - this is kind of an ugly hack. I would suggest to change the page content and use json_encode() in the "url" page, and parse the content using json_decode()