php get values from array json output - php

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!

Related

How to use array values as Variables in big multidimensional Array php

I have to access to all values stored in a big multidimensional array, here's an example of print_r the array:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
[Periodo] => Array
(
[#attributes] => Array
(
[FechaIngreso] => 1998-12-12
[FechaRetiro] => 2021-11-12
[FechaLiquidacionInicio] => 2021-05-01
[FechaLiquidacionFin] => 2021-05-30
[TiempoLaborado] => 10829
[FechaGen] => 2021-05-05
)
)
[Devengados] => Array
(
[Basico] => Array
(
[#attributes] => Array
(
[DiasTrabajados] => 30
[SueldoTrabajado] => 1258955.00
)
)
)
)
The thing I want to do is extract the values from that array, I have tried this way:
<?php
$cunenov = $array['Novedad']['#attributes']['CUNENov'];
but doesn't work..
Any suggests?. Thanks in advance.
I wanted you to see the "constructed" array and the outputs .. Stand alone, this php works .. Paste it into a stand alone php file and determine what you're doing differently to not achieve the same result.
Building out the array, and then print_r so you can validate it's the same structure as your array.
<?php
$test = Array();
$test['Novedad'] = array();
$test['Novedad']['#attributes'] = array();
$test['Novedad']['#attributes']['CUNENov'] = 4545454545;
print_r( $test );
Yields:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
)
Then we echo:
$cunenov = $test['Novedad']['#attributes']['CUNENov'];
echo "Value is $cunenov";
Yields
Value is 4545454545

Get Data from JSON decode Using Pgp

So i have a function that checks for reference in my system which is like a pyramid, i want to display who you invited and who he or she invited too, im using this foreach loop to get all reference under a user id and save them in json. but im having problem listing or extrating the results using php.
here is my pup recursive function to find all children or an entered user.
<?php
include 'includes/connect.php';
$menu['Users'] =getChildren(2);
echo '<pre>'; print_r($menu); // check tree array
function getChildren($parent_id)
{
$db=new mysqli('localhost','root','P=3h?9)Do#R#w2NQ','tobo'); //db
$refs=$db->query("SELECT * FROM users WHERE ref=$parent_id");
$children = array();
$i = 0;
foreach ($refs as $key => $downline) {
$children[$i] = array();
$children[$i]['fname'] = $downline['fname'];
$children[$i]['downline'] =getChildren($downline['id']);
$i++;
}
return $children;
}
$myJson = json_encode($menu);
$newJson = json_decode($myJson, true); ?>
it works fine and i get the following result:
Array
(
[Users] => Array
(
[0] => Array
(
[fname] => Andrey
[downline] => Array
(
[0] => Array
(
[fname] => Alisa
[downline] => Array
(
[0] => Array
(
[fname] => Maxim
[downline] => Array
(
[0] => Array
(
[fname] => james
[downline] => Array
(
)
)
)
)
)
)
)
)
[1] => Array
(
[fname] => Satori
[downline] => Array
(
)
)
)
)
my question is how can i get this result in a list using php so i can display the to the user as a list.
i tried to decode but im new to json so its still not usable by me. i hope you will be patient with my question structure, thank u very much

get specific values of a multidimensional array in php

I am searching for the best way to get specific values in a multidimensional array. My array looks like this. I dont know how many Arrays i get back so i need this in a foreach.
e.g. I want now to return from every array the [event_budget][0] value. How can i achieve this?
Array
(
[0] => Array
(
[event_budget] => Array
(
[0] => Bis € 4000
)
[event_date] => Array
(
[0] => 27.10.2019
)
[event_date_timestamp] => Array
(
[0] => 1572134400
)
)
[1] => Array
(
[event_budget] => Array
(
[0] => Bis € 500
)
[event_date] => Array
(
[0] => 29.10.2019
)
[event_date_timestamp] => Array
(
[0] => 1572307200
)
)
)
There are different ways in which you can get your required result.
The most basic :
$finalArray = array();
foreach($result as $key => $value){
$finalArray[] = $value['event_budget'][0];
}
The $finalArray will have your required result. You can also use like #rakesh have mentioned.
array_map('array_shift',array_column($a, 'event_budget'));
It would be great if you have control over the main array. If that is the case you can avoid event_budget to be an array.
If possible, it should be made like this:
[event_budget] => Array
(
[0] => Bis € 500
)
[event_budget] => Bis € 500
Hope it was helpful.
Thanks
You can use array_map with array_column and array_shift
$aa = array_map('array_shift',array_column($a, 'event_budget'));
print_r($aa);
Working example : https://3v4l.org/fKpjY
Start with:
print_r(array_column($array, 'event_budget'));
But as event_budget is array too, you need to get columns with index 0:
print_r(array_column(
array_column($array, 'event_budget'),
0
));
Full demo.

Extract specific values from JSON Array php

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.

Retrieving values from multi level array

Hi would love some help on how to perform this. Cause so far what I'm doing now is failing.
this is the sample output of json turned to array.
Array
(
[0] => Array
(
[0] => Array
(
[value] => lit-PR-00-Preparing-Precise-Polymer-Solutions.html
)
)
[1] => Array
(
[0] => Array
(
[value] => 90Plus
)
)
[2] => Array
(
[0] => Array
(
[value] => Particle Size Analyzer
)
)
)
So far this is what I got so far and It's still not outputting the value I need. Would appreciate some help on what I'm doing wrong thanks.
$decode = json_decode($row['elements'], true);
echo '<pre>';
//print_r($decode);
print_r(array_values($decode));
echo '</pre>';
echo ($value['0'][1][value]);
$decode = json_decode($row['elements'], true);
// iterate through array
foreach($decode as $array_row) {
echo $array_row[0]['value'];
}
// display specific row #2
echo $decode[2][0]['value'];
PHP Arrays

Categories