Jump to content

Tidying Up An Array


beanymanuk

Recommended Posts

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');

Link to comment
https://forums.phpfreaks.com/topic/271201-tidying-up-an-array/
Share on other sites

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');

Link to comment
https://forums.phpfreaks.com/topic/271201-tidying-up-an-array/#findComment-1395444
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.