How can I parse json array string to array on php - php

How can I parse json array string to array on php
'[{"a": "1", "b": "2"}, {"a": "3"}]'
seems json_decode allows parse only objects but not arrays. Should it be manually parsed to array before using json_decode?
Seems problem in string. I get a variable with json, and if I output it, looks like the json is valid
echo($jsonvar); //result [{"title":"Home","id":"/","url":"/"}]
but when I try parse string from the variable, the result is nothing even when string is trimmed
echo('[{"title":"Home","id":"/","url":"/"}]', true); //nice parsed array
echo($jsonvar, true); //nothing
echo(trim($jsonvar, " \t\n\r\0\x0B"), true); //nothing

Pass the true as a second parameter to your json_decode() to parse the json string to array.
$json='[{"a": "1", "b": "2"}, {"a": "3"}]';
$arr= json_decode($json,true);
print_r($arr);

You can get the json into array by using the true flag in json_decode()
<?php
$str = '[{"a": "1", "b": "2"}, {"a": "3"}]';
$arr=json_decode($str,true);
print_r($arr);

Related

PHP get array from string

I have an array in string given below.
$string="
"Status":true,
"ReVerifiedCount":1,
"ProfilePrefix":"INVTRK"
";
How I can get array from this string same as it exists in string.
<?php
$string='{
"Status":true,
"ReVerifiedCount":1,
"ProfilePrefix":"INVTRK"
}';
$data=json_decode($string,true);
print_r($data);
I formated your string-json the right way. Your double quotes and the missing brackets were creating the main problem as your input was not a valid json.
Output is this:
Array ( [Status] => 1 [ReVerifiedCount] => 1 [ProfilePrefix] => INVTRK )
First your String looks like a json string.
$string='{
"Status":true,
"ReVerifiedCount":1,
"ProfilePrefix":"INVTRK"
}';
This is the correct form.
To parse it use json_decode from PHP
$parsedArray = json_decode($string, true);
Here is a link to the doc : http://php.net/manual/en/function.json-decode.php

How to get data from associative array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
How to write PHP for get data from this.
{"room":[{"single_room":1,"twin_room":3}]}{"total_amount":[{"amount":20899}]}{"travelernum":[{"total":1}]}
I tried like this
$BookingDetail["room"]["single_room"];
but got result like this
Illegal string offset 'room'
and
Illegal string offset 'single_room'
How to solve it.
Try this ... you need to decode the json, you get a PHP object and then you can access each element ..
<?php
$str = '{"room":[{"single_room":1,"twin_room":3}]}{"total_amount":[{"amount":20899}]}{"travelernum":[{"total":1}]}';
$json = json_decode($str);
echo "<pre>";
print_r($json);
echo "</pre>";
echo $json->room->single_room;
By the way, I think your json is invalid. You can check this out at http://jsonlint.com/
The string you are using looks like JSON but it doesn't check out as valid. See JSONLint.com... Editing it to make it valid (single line)...
{"room": [{"single_room": 1, "twin_room": 3}], "total_amount": [{"amount": 20899}], "travelernum": [{"total": 1 }]}
OR (Multi-Line)...
{
"room": [{
"single_room": 1,
"twin_room": 3
}],
"total_amount": [{
"amount": 20899
}],
"travelernum": [{
"total": 1
}]
}
If this is the format all your strings are in, you can manipulate them into valid JSON by first by using...
$original_string = '{"room":[{"single_room":1,"twin_room":3}]}{"total_amount":[{"amount":20899}]}{"travelernum":[{"total":1}]}';
$new_string = str_replace("}{", ",", $original_string); // replace }{ with ,
Once your string is in valid JSON format, converting to a PHP array because rather simple, use something like this...
$BookingDetail = json_decode($new_string, true); //true param gets assoc array
I tested all the code above and it works.See PHP json_decode() function reference for more detail on converting JSON strings to PHP Arrays.
http://php.net/manual/en/function.json-decode.php

Can't decode JSON from file in PHP

I'm having troubles with json_decode in PHP:
I have this on file:
{1: ['oi','oi'], 2: ['foo','bar']}
And this is my php code:
<?php
$string = file_get_contents("quizen.json"); // the file
$json = json_decode($string);
echo $json[1][0]
?>
But the echo returns anything, I used var_dump, and I get NULL!
What's the problem?
The issue is that your file is not valid JSON since it uses single quotes for strings and has integers as object keys:
{1: ['oi','oi'], 2: ['foo','bar']}
Also, since the JSON is an object, you should decode it to an associative array using json_decode($string, true).
According to the JSON spec:
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.
Also, the object keys need to be strings.
If you change the single quotes to double quotes and edit your PHP's decode_json call to decode to an associative array, it should work. For example:
JSON:
{"1": ["oi","oi"], "2": ["foo","bar"]}
PHP:
<?php
$string = file_get_contents("quizen.json"); // the file
$json = json_decode($string, true); // set to true for associative array
echo $json["1"][0];
?>

Convert JSON String to Array

I have this JSON string
{"ticker":{"high":736.45099,"low":681,"avg":708.725495,"vol":13038780.63684,"vol_cur":18382.55965,"last":726,"buy":726,"sell":724.5,"updated":1388242741,"server_time":1388242743}}
How can i get the "last" parameter after doing json_decode()?
Do like this
<?php
$json='{"ticker":{"high":736.45099,"low":681,"avg":708.725495,"vol":13038780.63684,"vol_cur":18382.55965,"last":726,"buy":726,"sell":724.5,"updated":1388242741,"server_time":1388242743}}';
$json_arr=json_decode($json,true);
echo $json_arr['ticker']['server_time'];//"prints" 1388242743 which is the last param "server time"
Use json_decode with parameters json string and TRUE.When TRUE, returned objects will be converted into associative arrays.
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true));
?>
http://php.net/json_decode

How to remove unwanted quotes from JSON data ?

When I try to json encode an array using static value it output like :
[
{"data":[0,0,0,0,0,5],"name":"www.google.com"},
{"data":[0,0,0,0,0,4],"name":"www.yahoo.com"},
{"data":[0,0,0,0,85,0],"name":"www.bing.com"}
]
then I tried json encode using dynamic value it output like this
[
{"data":[0,0,0,0,0,"5"],"name":"www.google.com"},
{"data":[0,0,0,0,0,"4"],"name":"www.yahoo.com"},
{"data":[0,0,0,0,"85",0],"name":"www.bing.com"}
]
for non-zero value, there a extra double quotes ("") how remove it ?
You need parse with intval, example:
$arr = array("1", intval("2"));
echo json_encode($arr);
Outputs:
["1",2]

Categories