hyster Posted September 15, 2015 Share Posted September 15, 2015 im trying to loop the array but its throwing an error on the 500020315 saying its unexpected. echo a single entry works fine but I don't understand why it dosent like the number in the foreach thanks for any help $output = json_decode($json, true); // works fine with single entry echo $output['data']['500020315']['members']['3']['role']; // needs to be looped foreach ($output->data->500020315->members as $item) { echo $item->role . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/ Share on other sites More sharing options...
Ch0cu3r Posted September 15, 2015 Share Posted September 15, 2015 You need to use array notation. foreach ($output['data']['500020315']['members'] as $item) { echo $item->role . '<br>'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520899 Share on other sites More sharing options...
hyster Posted September 15, 2015 Author Share Posted September 15, 2015 thanks Ch0u3r Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520900 Share on other sites More sharing options...
hyster Posted September 15, 2015 Author Share Posted September 15, 2015 this code works fine but id like to sort by the account_name alphabetically. I have found the sort() function but the only examples I can find sort from the "1st" level of an array. sort($output) wont work as there is nothing to sort by I need something like sort($output['data']['500020315']['members']['account_name']) but when I try to use sort ill get an unexpected foreach $output = json_decode($json, true); echo '<table>'; foreach ($output['data']['500020315']['members'] as $item) { echo '<tr><td>'.$item['account_name'] . '</td></tr>'; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520907 Share on other sites More sharing options...
Barand Posted September 15, 2015 Share Posted September 15, 2015 Use a custom sort function with usort() http://forums.phpfreaks.com/topic/298103-how-to-sort-associative-array/?do=findComment&comment=1520543 Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520910 Share on other sites More sharing options...
hansford Posted September 16, 2015 Share Posted September 16, 2015 (edited) $output = array( 'data'=>array( '500020315'=>array( 'members'=>array( array('account_name'=>'zebra'), array('account_name'=>'gazelle'), array('account_name'=>'chiken'), array('account_name'=>'antelope') )))); usort($output['data']['500020315']['members'],function($a, $b) { $c = $a['account_name']; $d = $b['account_name']; if($c == $d) return 0; else if($c > $d) return 1; else return -1; }); Edited September 16, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520928 Share on other sites More sharing options...
Solution Barand Posted September 16, 2015 Solution Share Posted September 16, 2015 for string fields, just use strcmp() in the function (that's what it's for) usort($output['data']['500020315']['members'],function($a, $b) { return strcmp($a['account_name'], $b['account_name']) ; }); For numeric sorts, return $a - $b (To sort DESC, reverse a and b in the comparisons) 2 Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520933 Share on other sites More sharing options...
hyster Posted September 16, 2015 Author Share Posted September 16, 2015 thanks barand. as usual ur a star Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520937 Share on other sites More sharing options...
hyster Posted September 16, 2015 Author Share Posted September 16, 2015 I used strcasemp instead of strcmp Quote Link to comment https://forums.phpfreaks.com/topic/298181-looping-json-array/#findComment-1520938 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.