PHP json string variable - php

I want to decode a JSON string. However, the value that I got is something like this string(100) "{"data":{"type":"campaign-folders","id":"d208f3171a","attributes":{"name":"My Folder"}}}".
Could anyone tell me what should I do so that I can treat this variable as a usual string ?

Use json_decode http://php.net/manual/en/function.json-decode.php
$obj = json_decode($json);
var_dump($obj);

Use json_decode ,you can solve it.
var_dump(json_decode($json, true));//check this.

here to convert JSON string to Array
$someArray = json_decode($someJSON, true);
print_r($someArray);
echo $someArray[0]["name"];
and here to convert JSON to Object
$someObject = json_decode($someJSON);
print_r($someObject);
echo $someObject[0]->name;

Related

How convert $GLOBALS to string?

I need save $GLOBALS into a field in MySQL, but...
$GLOBALS is an ARRAY
When I try with some function as
function array_to_string($array){
$string = '';
code...
$string .= code...
code...
return $string;
}
$string = array_to_string($GLOBALS);
this "$string" grow and grow... (is infinite)
Any idea please?
you can try implode() function....
The implode() function returns a string from the elements of an array.
For example..
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
the output
Hello World! Beautiful Day!
Conversion to JSON is adviced.
$string=json_encode($array);
json_encode Returns a JSON encoded string on success or FALSE on failure.
$array_back=json_decode($string);
json_decode Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
Try to use utf8_encode and json_encode
$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);

Extracting data from Json in Php

I have this Json Object Below, I want to extract this data and output it in PHP
{"seat_booked":"A5","0":"A5","1":"A3"}
then get them into this format
$seat_booked = "'A5', 'A5', 'A3'";
How can I do this?
I hope you are looking for this, its very simple example by using json_decode():
$string = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = json_decode($string,true);
$resuiredString = '"'."'".implode("','", $decoded)."'".'"';
echo $resuiredString;
Result:
"'A5','A5','A3'"
Side Note:
I suggest you to learn about variable concatenation.
PHP Concatenation
Another solution:
$json = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = array_map(
function($val) {
return "'". $val."'";
},
array_values(json_decode($json, true))
);
To get an object from a json in php you can use json_decode has explained here.
But you have another problem, your json is wrong!
If you want to represent a single dimensional array you should at least do this
["A5","A5","A3"]
Finally, using json_decode:
$obj = json_decode('["A5","A5","A3"]');
var_dump($obj);
Also, you could do something like:
{"0":"A5","1":"A5","2":"A3"}
$obj = json_decode('{"0":"A5","1":"A3", "2": "A5"}', true);
var_dump($obj);
Edit:
It's not very clear from your question if you are trying to get back an object from a json or if you just want to get a string from it.
If what you need is an string then you don't even need json, you could do this by string manipulation and/or using regex.
But just for completeness, if a quoted comma separated string is what you need you can do this:
$array = json_decode('["A5","A5","A3"]');
$str = implode("','",$array);
$str = "'" . $str . "'";
var_dump($str);

Formating the json data in php

I have the json data in following format
["0","0","0","0","0","0","0","2","5","4","3","0"]
I want to convert the above data in to following format using php
[0,0,0,0,0,0,0,2,5,4,3,0]
How can i do this using php
Thanks
You can use array_map to typecast all items in the array to an integer with the callback intval.
For integers in an array:
$array = array_map('intval', json_decode('["0","0","0","0","0","0","0","2","5","4","3","0"]'));
Retrieve JSON from array:
echo json_encode($array);
$string = '["0","0","0","0","0","0","0","2","5","4","3","0"]';
echo str_replace('"','', $string);
Output is:
[0,0,0,0,0,0,0,2,5,4,3,0]

json can't be displayed as php array

The examples I checked seems simple but I can't figure it out what I'm doing wrong.
I'm trying to convert and display JSON text as object array in PHP.
$json = '{"color1":red, "color2":blue, "color3":yellow}';
$arr= json_decode($json, true);
print_r($arr);
It doesn't output anything. but when I print $json, the output is just fine
Well that's not json, try
$json = '{"color1":"red", "color2":"blue", "color3":"yellow"}';
$arr= json_decode($json, true);
print_r($arr);
notice the strings are quoted with ", also see http://json.org

decode url with php

how can I decode following query string with php?
t=%B1Z%2B%26k%C9%BF%B1%3Fh%3Fd%3F%9F%2Fa%90%3Ft%C5%8B%A0%3F-%F9s%D5d+%E2sJ-B%9DE%D0T%FA%A4.%93%AF%A05%98d%F9%85%CC%22H%3Fd%F9%9D%C3%22hE%8B%C1%D65%3F%A8%3A%25%24&charset=ISO-8859-1
I've already tried urldecode but I get following output
t=±Z+&kÉ¿±?h?d?Ÿ/a?tÅ‹ ?-ùsÕd âsJ-BEÐTú¤.“¯ 5˜dù…Ì"H?dùÃ"hE‹ÁÖ5?¨:%$&charset=ISO-8859-1
I've tried many ways but couldn't decode it
thanks in advance
Use parse_str to "decode" and parse your string, as in the example snippet further down in this post.
Read more about the function in php.net's manual:
PHP: parse_str - Manual
$data = "t=%B1Z%2B%26k%C9%BF%B1%3Fh%3Fd%3F%9F%2Fa%90%3Ft%C5%8B%A0%3F-%F9s%D5d+%E2sJ-B%9DE%D0T%FA%A4.%93%AF%A05%98d%F9%85%CC%22H%3Fd%F9%9D%C3%22hE%8B%C1%D65%3F%A8%3A%25%24&charset=ISO-8859-1";
parse_str ($data, $out);
print_r ($out);
Array
(
[t] => ±Z+&kÉ¿±?h?d?/a?tÅ ?-ùsÕd âsJ-BEÐTú¤.¯ 5dùÌ\"H?dùÃ\"hEÁÖ5?¨:%$
[charset] => ISO-8859-1
)
Read about urlencode and urldecode here
$output = urldecode($encoded);
Use urldecode!
$decoded = urldecode(substr($queryString,3));
maybe this one might help:
<?php
$variable = 'http%3A%2F%2Fsample.com';
$variable = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($variable));
$variable = html_entity_decode($variable,null,'UTF-8');
echo $variable;
?>
output will be : http://sample.com

Categories