Selecting a nested stdClass Object - php

Hi I was wondering if it is possible to grab:
Here is my current code:
<?php
include('php-riot-api.php');
$region = 'euw';
$grab_id = 19631093;
$instance = new riotapi($region);
$grab_dataB = $instance->getStats($grab_id);
$decode_dataB = json_decode($grab_dataB);
$grab_tier = $decode_data->{'aggregatedStats[7].Unranked[wins]'};
print_r($decode_dataB);
?>
This is my result:
http://2v2.lolnode.com/testing.php (http://pastebin.com/DrJDnuaC)
I would like to be able to get the numbered result for Unranked[wins] (which is [aggregatedStats] => stdClass Object ( ) ) [7])

You can get directly:
$grab_tier = $decode_dataB->playerStatSummaries[7]->wins;
But you won't know for sure that the 8'th key is the one you want, or ...
You can loop in your results:
$unranked = '';
foreach($decode_dataB->playerStatSummaries as $summary){
if($summary->playerStatSummaryType == 'Unranked'){
$unranked = $summary;
break;
}
}
print_r($unranked);
and get values, eg $unranked->wins
you can json_decode with 2'nd parameter true (object is transformed into array) and loop like a normal array.
$decode_dataB = json_decode($grab_dataB, true);

Related

Appending new data to object

I'm stuck trying to append new data to an stdClass object that I'm creating for an AMchart
I'm returning all the rows I want from the DB, then creating a new object and looping through the returned array, but rather than appending what I want to the end of the existing object, it just gets overwritten. PHP objects dont have an append or push method, so how do you accomplish this?
Here's what my code looks like. Am I missing something simple?
$sql = 'SELECT
count(*) as clients,
STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
SUM(wait_time) as total_wait_time
FROM tb_by_client
WHERE status = #qualifier
GROUP BY Appt_date';
$rows = $db->fetchAll($sql);
$chartObject = new stdClass();
foreach($rows as $row){
$row->average = round($row->total_wait_time / $row->clients);
$chartObject->date = $row->date;
$chartObject->average = $row->average;
}
$chartArray[] = $chartObject;
return json_encode($chartArray);
So instead of getting something that looks like this
[{"date":"2018-10-01","average":12},{"date":"2018-10-02","average":-33},{"date":"2018-10-04","average":23},{"date":"2018-10-05","average":6}]
I get back just a single
[{"date":"2018-10-01","average":12}]
Because each loop overwrites the last key and value
How do you append instead?
Your problem is you overwrite the data without saving it
$chartObject = new stdClass();
foreach($rows as $row){
$row->average = round($row->total_wait_time / $row->clients);
$chartObject->date = $row->date;
$chartObject->average = $row->average;
}
$chartArray[] = $chartObject;
See on each iteration of foreach($rows as $row){ you change the data in $chartObject, but you never save in your $chartArray.
Do this instead
foreach($rows as $row){
$chartObject = new stdClass(); //new instance of stdClass, obj pass by refrence
$row->average = round($row->total_wait_time / $row->clients);
$chartObject->date = $row->date;
$chartObject->average = $row->average;
$chartArray[] = $chartObject;
}
Personally I wouldn't even bother with using an object:
foreach($rows as $row){
$average = round($row->total_wait_time / $row->clients);
$chartArray[] = ['date'=>$row->date,'average'=>$average];
}
When you JSON Encode an array with string keys, it will make it the correct Javascript Object structure. So there really is no need to keep all those objects in memory and the code is much smaller, cleaner, and easier to read.
One last thing I hinted at in the code, is that objects are pass by reference in PHP (now), and if you don't create a new instance of the object for each iteration, you will actually update all references to the object. This can be illustrated like this:
$obj = new stdClass;
$objects = [];
for($i=0;$i<3;++$i){
$obj->foo = $i;
$objects[] = $obj;
print_r($objects);
}
Output:
Array
(
[0] => stdClass Object
(
[foo] => 0
)
)
Array
(
[0] => stdClass Object
(
[foo] => 1
)
[1] => stdClass Object
(
[foo] => 1
)
)
Array
(
[0] => stdClass Object
(
[foo] => 2
)
[1] => stdClass Object
(
[foo] => 2
)
[2] => stdClass Object
(
[foo] => 2
)
)
Sanbox
Each array is a single iteration of the for loop, this is the same array with another row added after each iteration.
As you can see each copy (its not really a copy) is updated by reference in the array. Basically we have stored the same object (instance ,will call him Bob) 3 times, instead of 3 separate objects (Bob, Alice, John).
If the data you stored was the color of a persons shirt, when Bob puts on a red shirt, he has a red shirt on, but Alice and John don't.
Because of this you need to create a new instance of the object for each iteration and store that.
Hope that helps!
You can do the maths in the SQL and it cuts out the loop altogether...
$sql = 'SELECT STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
round(SUM(wait_time)/count(*)) as average
FROM tb_by_client
WHERE status = #qualifier
GROUP BY Appt_date';
return json_encode($db->fetchAll($sql));
You're misunderstanding what should be in the loop and what shouldn't.
This should be fine:
$sql = 'SELECT
count(*) as clients,
STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
SUM(wait_time) as total_wait_time
FROM tb_by_client
WHERE status = #qualifier
GROUP BY Appt_date';
$rows = $db->fetchAll($sql);
$chartArray = [];
foreach($rows as $row){
$row->average = round($row->total_wait_time / $row->clients);
$chartObject = new stdClass();
$chartObject->date = $row->date;
$chartObject->average = $row->average;
$chartArray[] = $chartObject;
}
return json_encode($chartArray);

PHP - get value from JSON

Before i decode my JSON i get this result:
{
"1":[{"membership_id":1,"group_id":1,"user_id":1},
"2":[{"membership_id":3,"group_id":1,"user_id":2}
}
How would i specify that i want to select the one who has 'user_id' == 2 and return membership_id value?
My attempt, but i get undefined value 'user_id':
$myjson = json_decode($s_o, true);
foreach ($myjson as $key => $value){
if($value['user_id'] == $cid){
$mid = $value['membership_id'];
}
}
echo $mid;
Basically i guess i would first have to select the right object and go through it with the foreach, but here i got a bit lost in the situation.
Use Array-Functions:
$json = '{
"1":[{"membership_id":1,"group_id":1,"user_id":1}],
"2":[{"membership_id":3,"group_id":1,"user_id":2}]
}';
$array = json_decode($json, true);
$searchUserID = 2;
$filteredArray = array_filter($array, function($elem) use ($searchUserID){
return $searchUserID == $elem[0]['user_id'];
});
$mid = array_column(array_shift($filteredArray), 'membership_id')[0];
echo "Membership-ID: ".$mid;
array_filter uses a callback function that iterates over every element of the array. If the callback function returns true, that element is assigned to $filteredArray. No need for a foreach loop that way.
But the return value is the whole array element:
Array
(
[2] => Array
(
[0] => Array
(
[membership_id] => 3
[group_id] => 1
[user_id] => 2
)
)
)
So you have to extract your membership_id.
Read the following line from inside out.
First, we fetch the first entry of the array with array_shift (since we have only one entry, this will be our desired entry).
Array
(
[0] => Array
(
[membership_id] => 3
[group_id] => 1
[user_id] => 2
)
)
We pass this array on to array_column to find the entry in the encapsulated array with the column name membership_id. Since array_column again returns an array,
Array
(
[0] => 3
)
we get the (one and only) entry by adding [0] to the end of this command.
Since the last part is a little complicated, here's a torn apart version of it:
$firstEntryOfFilteredArray = array_shift($filteredArray);
$arrayWithValueOfColumnMembershipID = array_column($firstEntryOfFilteredArray, 'membership_id');
$membership_id = $arryWithValueOfColumnMembershipID[0];
These three lines are concatenated into this:
$mid = array_column(array_shift($filteredArray), 'membership_id')[0];
here's a working example: http://sandbox.onlinephpfunctions.com/code/8fe6ede71ca1e09dc68b2f3bec51743b27bf5303
I'm assuming the JSON actually looks like:
{
"1":[{"membership_id":1,"group_id":1,"user_id":1}],
"2":[{"membership_id":3,"group_id":1,"user_id":2}]
}
Each element of the object is an array for some reason. So you need to index it with $value[0] to access the object contained inside it.
$myjson = json_decode($s_o, true);
foreach ($myjson as $key => $value){
if($value[0]['user_id'] == $cid){
$mid = $value[0]['membership_id'];
break;
}
}
echo $mid;
If the arrays can contain multiple elements, you'll need nested loops.
$myjson = json_decode($s_o, true);
foreach ($myjson as $key => $value){
foreach ($value as $object) {
if($object['user_id'] == $cid){
$mid = $object['membership_id'];
break 2;
}
}
}
echo $mid;
This is a bit speculative, but I think the data is indexed by user ID. If that's the case, it makes the lookup much simpler.
After decoding with $myjson = json_decode($s_o, true);
Just find the record by ID and get the membership_id from the matching row.
$membership_id = reset($myjson['2'])['membership_id'];`
You should probably verify that that ID exists, so maybe something like:
$membership_id = isset($myjson['2']) ? reset($myjson['2'])['membership_id'] : null;
If I'm wrong and the fact that the row numbers match the user_id is just a coincidence, then never mind :)

Exclude the first result from an Array

I am currently messing around with iTunes Api and have ran into a problem with the returning process of albums track list returning the first result as the actual album data instead of the tracklist.
$loop['artist_name'] = $counted[$x]->artistName;
$loop['album_id'] = $counted[$x]->collectionId;
$loop['album_name'] = $counted[$x]->collectionName;
$loop['track_number'] = $counted[$x]->trackCount;
$loop['artwork_url'] = $counted[$x]->artworkUrl100;
$loop['copyright'] = $counted[$x]->copyright;
$loop['genre'] = $counted[$x]->primaryGenreName;
$loop['release_date'] = $counted[$x]->releaseDate;
$data_b = file_get_contents('https://itunes.apple.com/lookup?id='.$loop['album_id'].'&entity=song');
$response_ab = json_decode($data_b);
print '<pre>';
print_r($response_ab);
print '</pre>';
The above portion for print_r returns the following data.
[results] => Array
(
[0] => stdClass Object
(
[wrapperType] => collection
)
[1] => stdClass Object
(
[wrapperType] => track
)
Then continues onward down the track list, how can I remove the first [0] option from my loop? It appears this only returns the album name anyway when I am trying to get the tracklist.
I didn't test the below code however it should work.
$data_b = file_get_contents('https://itunes.apple.com/lookup?id='.$loop['album_id'].'&entity=song');
$response_ab = json_decode($data_b);
$count = $response_ab->results;
$arr = count($count);
for($a = 1; $a < $arr; $a++) {
// do some cool stuff here
}

php JSON_DECODE for different data length

From mysql, I have a data BLOB data type which has two different scenarios (Please see below). I am trying to put them into a string. Below is the process that I am doing:
1. Query:
$names= $results[0]->name;
print_r($names);
First scenario:
[{"Name":"Mike"},{"Name":"Sean"}]
Second scenario:
{"Name":"Mike Smith","Spaces":"1"}
2. JSON_DECODE
$data = json_decode(stripslashes($names),true);
print_r($data);
First scenario:
Array
(
[0] => Array
(
[Name] => Mike
)
[1] => Array
(
[Name] => Smith
)
)
Second scenario:
Array
(
[Name] => Mike Smith
[Spaces] => 1
)
3. What I am trying to do: To put them into a string
$string = '';
for ($i=0; $i <sizeof($data) ; $i++){
$row = $data[$i];
$name = $row -> Name;
if(isset($row -> Spaces)){
$number = '(' . $row -> Spaces . ')';
}else{
$number = '';
};
$string .= $name . $number . ', ';
};
//Remove last comma
$refined = rtrim($string,', ');
print_r($refined);
4. ISSUE
The issue I am having is that because the data can have two different scenarios like shown in the "1.Query", I can't predict or generalize it and getting errors like "Trying to get property of non-object".
How can I fix this?
Since you're passing true to the $assoc parameter of json_decode, $row->Name will never be the right syntax, since you have an array, and that's syntax for accessing objects; you want $row['Name'] instead. (It's unusual to put space around the -> by the way.)
However, you have basically the right idea on this line:
if(isset($row -> Spaces)){
For an array, that would instead by:
if(isset($row['Spaces'])){
You can do the same thing to see if you've got a name, or a list of names:
if(isset($row['Name'])) {
// In scenario B
echo $row['Name'];
// check for 'Spaces' etc
} else {
// In scenario A
foreach ( $row as $item ) {
echo $item['Name'];
}
}
Note my use of a foreach loop here, which is much neater than your for loop for this kind of thing.
Well I'll edit my answer for better understanding
$str1 = '[{"Name":"Mike"},{"Name":"Sean"}]';
$str2 = '{"Name":"Mike Smith","Spaces":"1"}';
$json1 = json_decode($str1, false);
$json2 = json_decode($str2, false);
if(is_object($json1))
{ echo 'json1 is object<br>'; } else
{ echo 'json1 is NOT object<br>'; }
if(is_object($json2))
{ echo 'json2 is object'; } else
{ echo 'json2 is NOT object'; }
http://php.net/manual/en/function.is-object.php

How do i properly add an element into an array depending on the elements have values or not in php

I'm trying to do something as below :
$location1 = (isset($_POST['location1'])) ? $_POST['location1'] : null ;
$location2 = (isset($_POST['location2'])) ? $_POST['location2'] : null ;
$locations = array($location1, $location2);
print_r($locations);
here print_r($locatios) showing Array ( [0] => location1 [1] => ) , an unnecessary index is being created if $location2 is null. I want if $location2 is null then i get just Array ( [0] => location1)
I think if statement will be better in this case.
$locations = array();
if (isset($_POST['location1'])) {
$locations[] = $_POST['location1'];
}
if (isset($_POST['location2'])) {
$locations[] = $_POST['location2'];
}
$locations = array();
if(isset($_POST['location1']) {
array_push($locations, $_POST['location1']);
}
if(isset($_POST['location2']) {
array_push($locations, $_POST['location2']);
}
print_r($locations);
Your logic was still inputting null into the array, which is why that index is there.
Name your form inputs with []:
<input name="locations[]">
<input name="locations[]">
Then you're already getting them as needed:
$locations = $_POST['locations'];
Optionally: filter out empty submitted fields:
$locations = array_filter($_POST['locations']);
Try an array_merge:
http://php.net/manual/en/function.array-merge.php
So:
$locations = array_merge($location1, $location2);
And this question deals with the issue when one of the arrays is empty: PHP array_merge if not empty
By checking if it's array first before merging:
if(is_array($location1)) {

Categories