I'm trying to write a loop that will list all the uid
in the JSON data. The uid
spans two categories (shirts and pants). Below is what I have so far. Any help is greatly appreciated.
Current Code and JSON
$url = 'http://foothillertech.com/student/webdesign/2018/2018benrud2/projects/retail/data2.json';
$jsonData = file_get_contents($url);
$data = json_decode($jsonData, true);
I can get each uid
manually with this - $data['shirts'][0]['uid']
, but I'm looking for a way to loop through the data to return all uid's from both the shirts and pants category.
Thank you!
This is the code that solved my problem.
Thank you @Don't Panic for your help!
foreach ($data as $item_type => $items) {
foreach ($items as $item) {
$uids[] = $item['uid'];
echo $uids[$i]."<br>";
$i++;
}
}
You can merge the second level arrays (shirts and pants keys) and take the uid column from the result of that.
$uids = array_column(array_merge(...array_values($data)), 'uid');
Details of this expression from inside out (PREVIOUS indicates result of previous step):
array_values($data)
converts the string keys of the outer array ('shirts', 'pants') to numeric array_merge(... PREVIOUS )
merges the two inner arrays, passing them to array_merge
using argument unpacking . (The previous array_values
step is needed because argument unpacking won't work on arrays with string keys.) array_column( PREVIOUS , 'uid')
takes all the 'uid' values from the merged array produced by the previous steps This is sort of a fancy way of doing something fairly simple, though. The code will be more clear if you just use a nested loop.
foreach ($data as $item_type => $items) {
foreach ($items as $item) {
$uids[] = $item['uid'];
}
}
As @jon-stirling mentioned it, you can use array_column()
to extract values from a sign column of an array. It does many others things that are beyond the scope of your question. Check it out at http://php.net/manual/en/function.array-column.php
Specifically for your example, the following code should give you all the uid
extracted from $data['shirts']
.
$url = 'http://foothillertech.com/student/webdesign/2018/2018benrud2/projects/retail/data2.json';
$jsonData = file_get_contents($url);
$data = json_decode($jsonData, true);
$shirtsUids = array_column($data['shirts'], 'uid');
$pantsUids = array_column($data['pants'], 'uid');
// Then you can use $shirtsUids and $pantsUids as you see fit.
Make sure that the $data array has a key called shirt or else you will have an error or warning thrown.
To avoid causing an error, I using use the Null coalescing operator ( ??
) to make uncertain values default to an acceptable one. So I will use $data['shirts'] ?? []
$data['shirts'] ?? []
. But again, make sure that $data['shirts'] is an array.
$url = 'http://foothillertech.com/student/webdesign/2018/2018benrud2/projects/retail/data2.json';
$jsonData = file_get_contents($url);
$data = json_decode($jsonData, true);
$shirtsUids = array_column($data['shirts'] ?? [], 'uid');
$pantsUids = array_column($data['pants'] ?? [], 'uid');
// Then you can use $shirtsUids and $pantsUids as you see fit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.