Echo JSON -> Arrays in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I've been trying to read a JSON string in PHP since yesterday, but I always fail. Can you help me?
{"WEAPON_PISTOL":{"ammo":227}}
TO
ECHO: WEAPON_PISTOL WITH 227 Ammunition
I've tried this way up until now:
$json_string = "{"WEAPON_PISTOL":{"ammo":227}}";
$obj = json_decode($json_string,true);
for($i=0;$i<count($obj);$i++) {
echo $obj[$i];
echo $obj[$i]["ammo];
}

The way to access the value of "ammo" is:
$json_string = '{"WEAPON_PISTOL":{"ammo":227}}';
$obj = json_decode($json_string);
echo $obj->WEAPON_PISTOL->ammo;
This returns:
227
If you want to loop through several weapons, even though your example only contains one weapon, you can do:
$json_string ='{"WEAPON_PISTOL":{"ammo":227},
"WEAPON_GUN":{"ammo":6}}';
$weapons = json_decode($json_string);
foreach ($weapons as $weaponName => $weapon) {
echo "$weaponName has $weapon->ammo rounds.\n";
}
This returns:
WEAPON_PISTOL has 227 rounds.
WEAPON_GUN has 6 rounds.

Related

unable to a decode_json in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have A Json data But I am unable to get desired response.
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
i want to capture some contents like
id , msisdncountrycode , msisdncountrycode , statuscode , hlrerrorcodeid
And Other Values As Well Please Help To Solve This problem .
check out this:
$result = json_decode($content);
var_dump($result->results[0]->id);//will get id
var_dump($result->results[0]->msisdncountrycode);//will get Country code
use this(works with multiple results).
$d = json_decode($content);
foreach($d->results as $row){
echo $row->id; //echoes 21390fb46e92
echo $row->msisdncountrycode; //echoes DE
echo $row->statuscode; //echoes HLRSTATUS_UNDELIVERED
echo $row->hlrerrorcodeid; //echoes 9
}
Echoes are based on current input. Since its in loop, it will print all values.
You can try this. Its working fine for me.
json_decode($content)
Try this:
<?php
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
echo '<pre>';
$jsonArr = json_decode($content, TRUE);
$outputArr = $jsonArr['results'][0];
print_r($outputArr);
$id = $outputArr['id'];
$msisdncountrycode = $outputArr['msisdncountrycode'];
$statuscode = $outputArr['statuscode'];
$hlrerrorcodeid = $outputArr['hlrerrorcodeid'];
?>

How to parse a specific json array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a php beginner. I've got this kind of JSON ?
{
"rows":
[
["/page1.php","568"],
["/","78"],["/page2.php","4"],
["/page2.php","2"],
["/page3.php","1"],
["/search.php","1"]
]
}
to parse json i use
<?php
$json_file = file_get_contents('test.json');
$parsed_json = json_decode($json_file);
?>
But i don't know to parse each page and number. Anyone can help me on this surely basic issue ?
thanks a lot
$parsed_json will be an object, whose rows property is an array. The elements of this array are arrays, whose first element is the page name, the second element is the page number:
foreach ($parsed_json->rows as $page) {
$page_name = $page[0];
$page_number = $page[1];
echo "Page #" . $page_number . " on " . $page_name . "<br>";
}

"\u00e1n" to "á" in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am receiving an external file with strings, I have no control over that file but I receive special characters as this (i think it's unicode?) sequence \u00e1n.
Is there anyway to convert this kind of char sequences to their "readable" counterparts?
Thanks!
EDIT:
I am calling a url that gives me a list with names of persons:
Tom\u00e1nsson\n
Eriksen\n
Gilverto\n
I am reading the names and showing them in my site.
EDIT
Now, I've figured out the correct, working answer based on the article I found. Here is it:
function unicode2utf8($str) {
$a=json_decode(str_replace("\n","<br>",'["'.$str.'"]'));
return str_replace("<br>","\n",$a[0]);
}
PHP:
echo unicode2utf8("Tom\u00e1nsson\n Eriksen\n Gilverto\n");
Output:
Tománsson
Eriksen
Gilverto
ORIGINAL
I've found an article about the same problem here ( http://www.welefen.com/php-unicode-to-utf8.html )
The solution is the following function
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
I've tried it with
echo unicode2utf8("\u00e1");
Output:
á

How can i identify position of MAX value in array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to get 112 position [01] and use this position[01] to compare something ? Can i do that ? Sorry about stupid question. I'm just a new guy really want to known. Thank for any help.
<?
$sql "SELECT * FROM truck WHERE t_ID = '001'";
$arry = odbc_fetch_array($DataExec);
echo "MAX value position's".$arry;
?>
echo $arry. This 1 i want to show [ MAX value position's 01.]. Or someone have better way.
<?php
$arr_numeric= array('41', '112', '25','54');
$max_position = array_search(max($arr_numeric), $arr_numeric);
echo $max_position;
?>
position start from 0.
OUTPUT
1
$arr = array('41', '112', '25','54');
$index = array_search(max($arr),$arr);
echo "MAX value position's".$index;
Similar Question here
I get it with a google search.
$maxs = array_keys($array, max($array))
Note:
this way you can retrieve every key related to a given max value.
If you are interested only in one key among all simply use $maxs[0]

Echoing JSON array in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After trying for a long period of time I am a bit stuck... I want to echo a value (url) in a json array with PHP and get it into a variable.
PHP:
$coverphotos = $facebook->api('http://graph.facebook.com/' . $album['id'] . '/picture?type=album&redirect=false&access_token=' . $access_token);
JSON:
{
"data": {
"url": "http://photos-c.ak.fbcdn.net/hphotos-ak-ash4/480080_567808343263483_176928672_a.jpg"
}
}
I want to get the actual url into a php variable... how would I go about doing this?
HOLY THAT TOOK LONG ENOUGH BUT I FIGURED IT OUT:
$coverphotos2 = $coverphotos['id'] . '?type=album&redirect=false&access_token=' . $access_token;
$content = file_get_contents($coverphotos2);
$photoarray = json_decode($content, TRUE);
$coverurl = $photoarray['data']['url'];
From the $coverphotos variable, you can store the URL of the picture with:
$picture = $coverphoto['id'];

Categories