Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.