[opgelost] PHP array rangschikken

Zelf bezig aan een modificatie? Wij kijken graag mee..
Forumregels

Sinds 1 januari 2009 wordt phpBB2 niet meer ondersteund.
Onderstaande informatie is verouderd en dient uitsluitend als archief.
phpBB2.0.x
Gesloten
Gebruikersavatar
k4r$t3n
Berichten: 3
Lid geworden op: 07 apr 2006, 10:50

[opgelost] PHP array rangschikken

Bericht door k4r$t3n » 20 dec 2006, 14:18

Hey iedereen, klein vraagje...
Ik vroeg me af of het mogelijk is om een array als de volgende te sorteren op volgens de 'lastMessage' index?

Code: Selecteer alles

$topic[0]['type'] = "F";
$topic[0]['title'] = "Lalala";
$topic[0]['author'] = 100001;
$topic[0]['lastMessage'] = "19 dec 2006 17:07:12";
$topic[0]['messages'] = 1;
$topic[0]['views'] = 31;

$topic[1]['type'] = "H";
$topic[1]['title'] = "Lilili";
$topic[1]['author'] = 100000;
$topic[1]['lastMessage'] = "19 dec 2006 12:51:40";
$topic[1]['messages'] = 1;
$topic[1]['views'] = 27;

$topic[2]['type'] = "H";
$topic[2]['title'] = "Lilili";
$topic[2]['author'] = 100000;
$topic[2]['lastMessage'] = "20 dec 2006 12:52:40";
$topic[2]['messages'] = 1;
$topic[2]['views'] = 27;

$topic[3]['type'] = "H";
$topic[3]['title'] = "Lilili";
$topic[3]['author'] = 100000;
$topic[3]['lastMessage'] = "17 oct 2006 11:52:40";
$topic[3]['messages'] = 1;
$topic[3]['views'] = 24;
M.a.w., de array $topic[3] zou $topic[0] worden, [1] => [1], [0] => [2] en [2] => 3...
Ik weet dat je er een scriptje voor kunt schrijven, maar ik vroeg me af of er een functie voor bestaat?

Tnx op voorhand :wink:

Gebruikersavatar
Ramon Fincken
Berichten: 2552
Lid geworden op: 27 nov 2005, 23:15
Locatie: Diemen
Contacteer:

Bericht door Ramon Fincken » 20 dec 2006, 21:08

als je je lastmessage als integer ( getal ) neerzet heb ik iets voor je
Freelance webdevelopment, including phpbb2 scripting!

Website founder van: phpBBinstallers.net phpBBantispam.com
Mods: zie op http://www.phpbb.com Blog in wording: RamonFincken.com

Gebruikersavatar
k4r$t3n
Berichten: 3
Lid geworden op: 07 apr 2006, 10:50

Bericht door k4r$t3n » 20 dec 2006, 23:10

Ramon Fincken schreef:als je je lastmessage als integer ( getal ) neerzet heb ik iets voor je
OK, ik wacht vol spanning :wink: :lol:

Gebruikersavatar
Ramon Fincken
Berichten: 2552
Lid geworden op: 27 nov 2005, 23:15
Locatie: Diemen
Contacteer:

Bericht door Ramon Fincken » 20 dec 2006, 23:16

Voorbeeld, ik wil de children_array hier sorteren op "eenheid_int" en wel oplopend:


aanmaken:

Code: Selecteer alles

               
                  $children_array = array();
                  while($row = mysql_fetch_array($result)) {
                                    $children_array[] = array('id' => $row['id'],'eenheid_int' => intval($row['eenheid']), 'eenheid_str' => $row['eenheid'], 'prijs' =>$row['prijs']);
                  }

$children_array2 = sortmddata($children_array,'eenheid_int','ASC','num');

de functie:

Code: Selecteer alles

///////////////////////////////////////////////////////////////////////////////////////////////////////
// sort

function sortmddata($array, $by, $order, $type){

  //$array: the array you want to sort
  //$by: the associative array name that is one level deep
  ////example: name
  //$order: ASC or DESC
  //$type: num or str

  $sortby = "sort$by"; //This sets up what you are sorting by

  $firstval = current($array); //Pulls over the first array

  $vals = array_keys($firstval); //Grabs the associate Arrays

  foreach ($vals as $init){
     $keyname = "sort$init";
     $$keyname = array();
  }
  //This was strange because I had problems adding
  //Multiple arrays into a variable variable
  //I got it to work by initializing the variable variables as arrays
  //Before I went any further

  foreach ($array as $key => $row) {

     foreach ($vals as $names){
        $keyname = "sort$names";
        $test = array();
        $test[$key] = $row[$names];
        $$keyname = array_merge($$keyname,$test);

     }
   }

   //This will create dynamic mini arrays so that I can perform
   //the array multisort with no problem
   //Notice the temp array... I had to do that because I
   //cannot assign additional array elements to a
   //varaiable variable

   if ($order == "DESC"){
     if ($type == "num"){
     array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
     } else {
     array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
     }
   } else {
     if ($type == "num"){
        array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
     } else {
       array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
     }
   }

   //This just goed through and asks the additional arguments
   //What they are doing and are doing variations of
   //the multisort

   return $array;
}
// sort
///////////////////////////////////////////////////////////////////////////////////////////////////////
Freelance webdevelopment, including phpbb2 scripting!

Website founder van: phpBBinstallers.net phpBBantispam.com
Mods: zie op http://www.phpbb.com Blog in wording: RamonFincken.com

Gebruikersavatar
k4r$t3n
Berichten: 3
Lid geworden op: 07 apr 2006, 10:50

Gracias!

Bericht door k4r$t3n » 20 dec 2006, 23:41

Tnx Ramon!
Juist wat ik nodig had... :thumb:
Srr trouwens voor de foute locatie van de post 8) :oops: :wink:

Adios :bier:

Gebruikersavatar
Ramon Fincken
Berichten: 2552
Lid geworden op: 27 nov 2005, 23:15
Locatie: Diemen
Contacteer:

Re: Gracias!

Bericht door Ramon Fincken » 21 dec 2006, 01:04

k4r$t3n schreef:Tnx Ramon!
Juist wat ik nodig had... :thumb:
Srr trouwens voor de foute locatie van de post 8) :oops: :wink:

Adios :bier:
Geeft niet, nu weet je t voor een volgende keer :)

Fijn dat dit is wat je nodig had !!
Freelance webdevelopment, including phpbb2 scripting!

Website founder van: phpBBinstallers.net phpBBantispam.com
Mods: zie op http://www.phpbb.com Blog in wording: RamonFincken.com

Gesloten