beanymanuk Posted November 26, 2012 Share Posted November 26, 2012 Hi I'm not the best at arrays and have got thecode below which is pulling values out based on posted in variables eg mountains, nightlife, I'm then adding them together per country the ratings for these for each country and them sorting the values to leave me with an array showing highest rated island at the first in the array Is there a more efficient way of doing the same thing as I have done below? Thankyou for everyones help in advance <?php //posted values in $tripw = $_POST['trip']; $entries = array(); //loop over islands and pull all ratings and category names out ?> {exp:channel:entries channel="islands"} {rating_level} <?php $island = "{url_title}"; $rate_num = array( 'catname' => "{rate_cat}{title}{/rate_cat}", 'rating' => "{rate_num}"); $entries[$island][] = $rate_num; ?> {/rating_level} {/exp:channel:entries} <?php //loop over values and pull out ones which match the posted categories and add together to give each islands a total $total = ""; foreach ($entries as $key => $val) { foreach($val as $subarray) { //pull in posted categories foreach($tripw as $tt2){ if($subarray['catname'] == $tt2) { $total += $subarray['rating']; } } } $topr[] = array('island' => $key,'total'=> $total); $total = 0; } //sort array highest to lowest based on total of each island function subval_sort( $a, $subkey, $order='dec' ) { foreach( $a as $k=>$v ) $b[$k] = strtolower( $v[$subkey] ); if( $order === 'dec' ) arsort( $b ); else asort( $b ); foreach( $b as $key=>$val ) $c[$key] = $a[$key]; return $c; } $topr = subval_sort($topr,'total'); Quote Link to comment https://forums.phpfreaks.com/topic/271201-tidying-up-an-array/ Share on other sites More sharing options...
beanymanuk Posted November 27, 2012 Author Share Posted November 27, 2012 Anyone got any hints tips on how I can make this better? Hi I'm not the best at arrays and have got thecode below which is pulling values out based on posted in variables eg mountains, nightlife, I'm then adding them together per country the ratings for these for each country and them sorting the values to leave me with an array showing highest rated island at the first in the array Is there a more efficient way of doing the same thing as I have done below? Thankyou for everyones help in advance <?php //posted values in $tripw = $_POST['trip']; $entries = array(); //loop over islands and pull all ratings and category names out ?> {exp:channel:entries channel="islands"} {rating_level} <?php $island = "{url_title}"; $rate_num = array( 'catname' => "{rate_cat}{title}{/rate_cat}", 'rating' => "{rate_num}"); $entries[$island][] = $rate_num; ?> {/rating_level} {/exp:channel:entries} <?php //loop over values and pull out ones which match the posted categories and add together to give each islands a total $total = ""; foreach ($entries as $key => $val) { foreach($val as $subarray) { //pull in posted categories foreach($tripw as $tt2){ if($subarray['catname'] == $tt2) { $total += $subarray['rating']; } } } $topr[] = array('island' => $key,'total'=> $total); $total = 0; } //sort array highest to lowest based on total of each island function subval_sort( $a, $subkey, $order='dec' ) { foreach( $a as $k=>$v ) $b[$k] = strtolower( $v[$subkey] ); if( $order === 'dec' ) arsort( $b ); else asort( $b ); foreach( $b as $key=>$val ) $c[$key] = $a[$key]; return $c; } $topr = subval_sort($topr,'total'); Quote Link to comment https://forums.phpfreaks.com/topic/271201-tidying-up-an-array/#findComment-1395444 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.