This question already has answers here:
json_decode to array
(12 answers)
How can I access an array/object?
(6 answers)
Closed 11 months ago.
I have a script which records your WAN and LAN IP, but it also has a bunch of unnecessary characters around the printed answer. How can I split the IPs and get rid of these characters?
Printed Answer: {"ip":[["WANIP","LANIP"]]}
What I want is 2 different variables, 1 to print wan and 1 to print lan.
I've tried with str_split and explode, maybe I didn't do it right or I can't do it with these, so any answers would help.
$json = '{"ip":[["WANIP","LANIP"]]}';
$decoded = json_decode($json, true); // true means it will format it into an assoc array
Then you'll be able to access your wanted strings by simply using $decoded['ip'][0][0] and $decoded['ip'][0][1] .
It looks like your "printed answer" is JSON. In which case parse the json then extract the needed values.
You could use something like json_decode()
try this code:
$data = "{\"ip\":[[\"WANIP\",\"LANIP\"]]}";
$jdecode = json_decode($data,true);
echo $jdecode['ip'][0][0];
echo $jdecode['ip'][0][1];
Hope this helps :)
I would first strip out some of the unnecessary clutter, then explode:
$response = '{"ip":[["WANIP","LANIP"]]}'; //or however you load your variable
$arryRemove = array('"', 'ip:[[', ']]}'); //specify an array of things to remove
$response = str_replace($arryRemove, "", $response);
$arryIPs = explode(",", $response);
$arryIPs[0] will contain WANIP,
$arryIPs[1] will contain LANIP
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I Have a string like this
{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}-
I need the value of Balance.
I tried like this.
$string = '{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":1,"Code":1,"Message":"Success"}-';
$withCharacter = strstr($string, 'Balance":');
echo substr($withCharacter, 1);
Tried to use explode also but no luck.
This seems like a valid JSON, why not json_decode and find the value:
$i = json_decode('{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}');
echo $i->Account->Balance;
This question already has answers here:
How to display json values that are in 2nd level of a multi-dim array?
(3 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Need help. I am having trouble solving this json thingy. here's what is on my database
{"img":["images/logo2.png","images/logo.png"]}
I need to decode this images and show it like this
images/logo2.png
images/logo.png
A quick google search will give you this link: json_decode.
$someJson = '{"img":["images/logo2.png","images/logo.png"]}';
$encodeJson = json_decode($someJson, true);
echo $encodeJson['img'][0]; //images/logo2.png
echo $encodeJson['img'][1]; //images/logo.png
Hint: Having a look at the PHP-Manual is not prohibited. ;)
Maybe select your entry from database and put in a variable called $dataBaseEntry then copy paste this code?
$decoded = json_decode($dataBaseEntry, true); //true for associated array
$logos = $decoded['img'];
print_r($logos)
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want to apply the regex to extract only the values. I am not getting the perfect one any help
[{"name":"basket ball"},{"name":"foot ball"},{"name":"sports"}]
There's absolutely no need for a regex here. Use json_decode():
$string = '[{"name":"basket ball"},{"name":"foot ball"},{"name":"sports"}]';
$data = json_decode($string, true);
now you have a normal php array $data to get your wanted data from.
like
echo $data[0]['name']; // basket ball
This question already has answers here:
Storing PHP arrays in cookies
(9 answers)
Closed 1 year ago.
I am converting the array into cookie by php serialize function
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", serialize($Promotedcart), time()+604800,'/');
And when the cookie is created then i am using the unserialize php function.
print_r(unserialize($_COOKIE['Promotedcart']));
it does not work.
When I print_R($_COOKIE) then it show me the value.
Cookies separated by semicolon. Serialized strings with arrays contain them inside. Maybe this is a problem. You can use base64 to avoid all possible escape issues.
You can use json_encode, json_decode functions to achieve this as an alternative.
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", json_encode($Promotedcart), time()+604800,'/');
$result = json_decode($_COOKIE['Promotedcart'], true);
print_r($result);
Give it a try, this should work.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Extract JSONP Resultset in PHP
I get the response in the following format. Im facing trouble on how to get inside "Plugin" variable and access other variables inside it.I used json_decode(), but i cant access the variables.
Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)
I tried
$a = json_decode($json,true);
echo $a['plugin_a'];
I dont get any output.
echo var_dump($json); gives me
string 'Plugin({
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_ce":"abc" })'
try substr();
http://sandbox.phpcode.eu/g/40c20.php
<?php
$json = substr('Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)', 9, -1);
print_r(json_decode($json));
Perhaps this will work for you:
$data=array('plugin_a'=>'abc','plugin_b'=>'bcd','plugin_c'=>'cde');
$json='{"Plugin":'.json_encode($data).'}';
$a=json_decode($json,true);
echo $a['Plugin']['plugin_a'];
It appears as though the actual json array may not have integrity. If this solution doesn't fit, can you post the code that actually builds the json array?