Help with Looping through an object - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Help with Looping through an object (/showthread.php?tid=72137) Pages:
1
2
|
Help with Looping through an object - enelson - 11-10-2018 Hi all, i have a JSON that looks similar to one shown below: Code: products: [ I want to loop through it and display in a select tag. I've failed to do that so far. To test I converted the JSON to PHP objects using $list = json_decode), then: PHP Code: for ($i=0; $i < count($list); $i++) { Code: A PHP Error was encountered PHP Code: for ($i=0; $i < count($list); $i++) { Code: An uncaught Exception was encountered PHP Code: for ($i=0; $i < count($list); $i++) { My question, how can I get id, pur_currency, etc... from the $list variable. Your answer is appreciated. RE: Help with Looping through an object - jreklund - 11-10-2018 It should be used like this as your JSON are stored as objects inside an array. {$list[$i]->id} $list = array with multiple arrays. $list[$i] = returns an object. $list[$i]->id = returns the ID inside that object. Personally I would just use a foreach loop instead. PHP Code: foreach($list as $row) { In case you don't need the position in that array. RE: Help with Looping through an object - paulkd - 11-11-2018 Hi, Your json format is incorrect. json keys should be in double quotes. It should look like this: Code: { Decode as normal Code: $list = json_decode($json); Your for loop would then iterate like this: Code: for ($i=0; $i < count($list->products); $i++) { or, as jreklund points out, foreach is the best way to iterate: Code: foreach ($list->products as $key => $row) { RE: Help with Looping through an object - InsiteFX - 11-11-2018 It is faster to get the count first before running a for loop. Otherwise you are calling the count method on each loop irritation. PHP Code: $count = count($list->products); RE: Help with Looping through an object - enelson - 11-11-2018 (11-11-2018, 12:52 AM)paulkd Wrote: Hi, I removed some part of the JSON before posting here - that inclusive. Thanks all for your replies, they were helpful. RE: Help with Looping through an object - enelson - 11-11-2018 Another issue I have is that I want to sort $list in ascending order by "data_amount". asort(), ksort() etc are returning bool(true) instead of the $list objects - and they should be. Any ideas as to how I can sort the $object? RE: Help with Looping through an object - paulkd - 11-11-2018 Do you have control of what is producing the json? If so, maybe you can use that process to sort (e.g. SQL query) RE: Help with Looping through an object - enelson - 11-11-2018 (11-11-2018, 10:43 AM)paulkd Wrote: Do you have control of what is producing the json?No, I'am receiving it via API call RE: Help with Looping through an object - InsiteFX - 11-11-2018 You could decode the json into an associated array sort it and then encode it back to json and then create your list. RE: Help with Looping through an object - paulkd - 11-12-2018 Hi, I got this answer from https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields Code: $list = json_decode($json); |