How to convert the encoded value into another format - php

YTo2OntpOjA7czo0OiIyMDY3IjtpOjE7czo0OiIyMDY4IjtpOjI7czo0OiIyMDY5IjtpOjM7czo0OiIyMDcwIjtpOjQ7czo0OiIyMDcxIjtpOjU7czo0OiIyMDcyIjt9
The above is my encoded data.
When I try to decode it I am getting the output in this format:
a:6:{i:0;s:4:"2067";i:1;s:4:"2068";i:2;s:4:"2069";i:3;s:4:"2070";i:4;s:4:"2071";i:5;s:4:"2072";}
How can I convert it into this
["2067","2068","2069","2070","2071","2072"]
or this
(2067,2068,2069,2070,2071,2072)
?

The data in your second block is serialised in PHP's object serialisation format. You can use the unserialize function to turn it back into a PHP variable:
$data = 'a:6:{i:0;s:4:"2067";i:1;s:4:"2068";i:2;s:4:"2069";i:3;s:4:"2070";i:4;s:4:"2071";i:5;s:4:"2072";}';
$obj = unserialize($data);
echo json_encode($obj);
This outputs
["2067","2068","2069","2070","2071","2072"]
which is an array in the JSON format you requested.
Live demo: http://sandbox.onlinephpfunctions.com/code/6e9841c72950d44abd0ec6d45e2815cdcc89f42d

Related

php - convert mysql data into json object

I using codeigniter. I want to retrive data from database and convert it into JSON object not JSON array.I'm using following code
public function json()
{
$content = $this->db->get('todolist'); //todolist table name
$data = $content->result_array();
echo json_encode($data);
}
Above code is converting database into JSON array.
Output
[{"todo_id":"1","todo_content":"Homework","date":"2016-05-05","iscomplete":null,"imagelink":"Lighthouse.jpg"},{"todo_id":"2","todo_content":"exam","date":"2015-04-21","iscomplete":null,"imagelink":"Desert.jpg"},{"todo_id":"3","todo_content":"Lab report","date":"2014-08-29","iscomplete":null,"imagelink":"FB_IMG_14700753538617403.jpg"}]
What will be best way to convert it into JSON object
Sangam try to understand the concept, check the following line:
$data = $content->result_array(); // $data is an array
echo json_encode($data); // here you are converting it into an json object
Your $data array contains more than one index in it that's why the json object is having multiple {} inside [];
You want to json_encode($data, JSON_FORCE_OBJECT).
The JSON_FORCE_OBJECT flag, as the name implies, forces the json output to be an object, even when it otherwise would normally be represented as an array.
Refer: PHP Array to Json Object
You can use JSON_FORCE_OBJECT see the example below.
echo json_encode($data, JSON_FORCE_OBJECT);
Assuming you're only getting one row back from your query.
change
echo json_encode($data);
to
echo json_encode($data[0]);

How to remove slashes from array key using PHP?

I am getting some slashes inside my JSON array key using PHP. I am providing my code below.
$result[] = $fcm->send_fcm_notify($device_id, $message);
echo json_encode($result);
The output of the above code is below:
{\"multicast_id\":7339396188598826217,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482327583160431%2d865361f9fd7ecd\"}]}"]
I need to remove the slashes, because I need to check success==1.
Use stripslashes() first on response then do json_decode()
Try
$json = json_decode(stripslashes('{\"multicast_id\":7339396188598826217,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482327583160431%2d865361f9fd7ecd\"}]}'));
if( $json->success == '1' )
{
echo "I got it";
}
Output
You can use json_decode() to conrect received json string into multidimensional array then you can use json_encode() to print that array in json format without any slashes.
But make sure your received json string is valid as i can see the json code is not valid this will throw error when you use json_decode.
Your json string is wrong so json_encode will not work i have removed extra ] at the end of string so that json can parse without any error.
<?php
$json_code = "{\"multicast_id\":7339396188598826217,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482327583160431%2d865361f9fd7ecd\"}]}";
$result = json_decode($json_code,true);
//for json output use json_encode()
echo json_encode($code);
?>

Unable to convert json response to php variable

I need to convert json file and pass it to php variables so that i can store them in the database.
<?php
//read the json file contents
$fo=fopen("dealdetails.json","r");
$fr=fread($fo,filesize("dealdetails.json"));
$array=json_decode($fr,true);
//$jsondata = file_get_contents('dealdetails.json');
//convert json object to php associative array
//$data = json_decode($jsondata, true);
echo $array;
echo $array['promo_id'];
echo "json not printed";
?>
I tried both file_get_contents and fopen, fread
Output :
Arrayjson not printed
I am not getting the value for promo_id instead when i printed the $array i am getting output as Array.
Please suggest. Thanks

How to insert an array in a json object in PHP

I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.

json to php array cdnjs

I am trying to convert cdnjs api JSON result to php array from this link. Here is my code:
<pre>
<?php
$cdnLinks = file_get_contents('http://api.cdnjs.com/libraries');
$cdnLinks = json_encode($cdnLinks);
$j = json_decode($cdnLinks);
print_r($j);
?>
</pre>
What am I missing? Thanks
This is what you are doing:
Download a string of JSON
Encode that string into JSON (you now have a JSON string containing a JSON object)
Decode that JSON back to a string of JSON
Print the string
You need to skip step 2.
Skip step 2 because the output is already a json string.
To decode a json into an array you also need to use second parameter as true json_decode($jsonObj, true).
Thanks

Categories