When I use the code below:
print_r($jsoni);
$badge_url = "http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=841370%3Fkey&steamids=76561198108211948&fbclid=IwAR0B4wUlosbqFElHBJw-AkLwb3mGsv42xKdtrEAarDmD97Ur3AprrkW4tCk";
$jsoni = json_decode(file_get_contents($badge_url), true);
I get the following as a result:
Array
(
[achievementpercentages] => Array
(
[achievements] => Array
(
[0] => Array
(
[name] => GAME_GREEN_LIGHT
[percent] => 70.9000015259
)
[1] => Array
(
[name] => CAREER_EARN_BADGE
[percent] => 62.2999992371
)
How can I get it so that it only shows the name and the percentage?
print_r($jsoni['achievementpercentages']['achievements'])
You could loop through the decoded data to create an associative array with the achievement names as keys and the percentages as values:
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
Which outputs:
Array
(
[GAME_GREEN_LIGHT] => 70.9000015259,
[CAREER_EARN_BADGE] => 62.2999992371
)
So im now using the following code.
<?php
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
print_r($achievements);
?>
wich results in
Array ( [GAME_GREEN_LIGHT] => 70.9000015259 [CAREER_EARN_BADGE] => 62.2999992371
how do i get it to show a list from top to bottom instead of in a cluster of text?
Thanks for the help!
I have a JSON Array
[0] => Array
(
[stage_id] => 80
[yieldVal] => Array
(
[0] => Array
(
[datajson] => [{"name":"doi","value":"215"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]
)
[1] => Array
(
[datajson] => [{"name":"doi","value":"698"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]
)
)
)
I need to extract the values from this Array
[0] => Array
(
[stage_id] => 80
[yieldVal] => Array
(
[doi_value] => 215
[doi_value] => 698
)
)
I have tried decoding the JSON. But unable to continue further.
$phpArray = json_decode($res['datajson'], true);
How to extract the values and assign the key.
EDIT : My final output should be
[0] => Array
(
[stage_id] => 80
[yieldVal] => 913 //215+698 -> Extracting values from [datajson]
)
One thing that may of tripped you up is that your datajson string is:
`[{"name":"doi","value":"215"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]`
The square brackets mean that json_decode will create an array from the objects.
Anyway, try this...should give you the exact output you asked for:
$yieldVal = 0;
foreach ($res['yieldVal'] as $key => $arr) {
$decode = json_decode($arr['datajson']);
$yieldVal = $yieldVal + $decode[0]->value;
}
$newArray = array (
'stage_id' => $res['stage_id'],
'yieldVal' => $yieldVal
);
//var_dump($newArray);
echo "<pre>".print_r($newArray, true)."</pre>";
You should be able to get the value with:
$doi_value = $phpArray[0]['value'];
You can then sum them, push them onto a resulting array, or whatever.
I have tried to use PHP decode to parse my JSON string below into an array so I can extract the current and day for both channels from the file.
my json file owl_output.json looks like..
{"channels":{"0":[{"current":1288,"units":"w"},{"day":31278.57,"units":"wh"}],"1": [{"current":660,"units":"w"},{"day":9191.11,"units":"wh"}]}}
I'am only ever getting one result displayed, the php code I have managed to get working is below
<?php
$string = file_get_contents('owl_output.json');
$data = json_decode($string,true);
print_r($json);
foreach ($data['channels']['0'] as $data)
{
echo $data ['current'];
}
?>
This only display the current for channel 0. If I try to add additional fields it doesn't display
echo $data ['current']['day']; ( doesn't work )
Can someone advise how I can display current and day for both channels 0 & 1 ?
My aim is to display this in a html page at the end and to keep polling the json file?
The array it displays is below
Array
(
[channels] => Array
(
[0] => Array
(
[0] => Array
(
[current] => 1288
[units] => w
)
[1] => Array
(
[day] => 31278.57
[units] => wh
)
)
[1] => Array
(
[0] => Array
(
[current] => 660
[units] => w
)
[1] => Array
(
[day] => 9191.11
[units] => wh
)
)
)
)
Can anyone offer any assistance with this ?
Thanks
The variable $data is conflicting:
Used to store the data, and used in the foreach loop. Rename the $data variable in the foreach for example:
<?php
$string = file_get_contents('owl_output.json');
$data = json_decode($string,true);
print_r($json);
foreach ($data['channels'] as $channel)
{
echo $channel[0]['current'];
echo $channel[1]['day'];
}
?>
I did edit since there was an other error because there is not 'current' in every record.
Conflict on $data reference in loop and bad array indexes :
foreach ($data['channels'] as $channel)
{
echo $channel[0]['current'];
echo $channel[1]['day'];
}
foreach ($data['channels'] as $chanel)
{
echo $chanel[0]['current'];
echo $chanel[1]['day'];
}
I need some help. I have a variable containing this string;
[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]
I need to retrieve the id and value for each pair and make it any array in PHP.
You can use json_decode and pass the second param as true so it returns an array like this
$json = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$decoded = json_decode($json,true);
print_r($decoded);
Working Example
Output would be
Array
(
[0] => Array
(
[id] => 17
[value] => 123456789
)
[1] => Array
(
[id] => 18
[value] => 2012-06-13
)
[2] => Array
(
[id] => 19
[value] => Kampala
)
[3] => Array
(
[id] => 20
[value] => 1
)
.......
)
Which you can loop through using foreach like.
foreach($decoded as $de){
// access id with $de['id']
// access value with $de['value']
}
You have got an json string. You can convert it to an array by using function json_decode
Check this code .
$str = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"}, {"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$array = json_decode($str);
foreach($array as $temp){
echo "ID : ".$temp->id."\t Value: ".$temp->value;
echo "<br />";
}
I have a list of items I want to feed to an API via an array but it is built from a separate object. I was thinking I could loop through the items in the object to construct a variable I could then feed to the array but something is disconnected. This is probably easier to see than it is to explain.
The code I am using is this:
//Set up the parser object
$parser = new XMLParser($xml);
$parser->Parse();
$skuList = '';
// Pull the inventory of the requested SKUs from Magento for comparison later
foreach($parser->document->product as $product)
{
$skuList .= "'" . $product->sku[0]->tagData . "',";
}
echo $skuList;
print_r( $proxy->call($sessionId, 'product_stock.list', array(array($skuList))));
If I run this at the command line I get
'1DAFPOT5','8GAIL','26BULK30',Array
(
)
Now if I change the print_r line by putting the contents of the variable directly in the call like this
print_r( $proxy->call($sessionId, 'product_stock.list', array(array('1DAFPOT5','8GAIL','26BULK30', ))));
I get this output which is what I am looking for
'1DAFPOT5','8GAIL','26BULK30',Array
(
[0] => Array
(
[product_id] => 2154
[sku] => 26BULK30
[qty] => 19.0000
[is_in_stock] => 1
)
[1] => Array
(
[product_id] => 2255
[sku] => 8GAIL
[qty] => 16.0000
[is_in_stock] => 1
)
[2] => Array
(
[product_id] => 2270
[sku] => 1DAFPOT5
[qty] => 23.0000
[is_in_stock] => 1
)
)
Am I constructing the variable incorrectly or do I need to feed it to the array differently?
$skuList looks like an array, but is still a string.
You have to do this after foreach loop:
$skuList = explode(',',$skulist);
Or, better, make skuList an array since beginnig:
$skuList = array();
foreach($parser->document->product as $product)
{
$skuList[] = $product->sku[0]->tagData;
}
print_r( $proxy->call($sessionId, 'product_stock.list', array($skuList)));
http://www.php.net/manual/en/function.explode.php