phpbar.de logo

Mailinglisten-Archive

[php] array_multisort

[php] array_multisort

Daniel Strathemeier php_(at)_phpcenter.de
Thu, 17 May 2001 07:51:59 +0200


Hallo Marko,


> Hallo Liste,
>
>   Ich hab jetzt ein bißchen mit der Funktion array_multisort rumgespielt
>   und konnte meinen multidimensionalen Array auch sortieren, allerdings
>   nicht so wie ich das will.
>
>   $a=array(
>     0 => array (2,"Zwei",30),
>     1 => array (1,"eins",20),
>     2 => array (4,"vier",10),
>     3 => array (3,"drei",40)
>   );
>   array_multisort($a);
>

ich habe gestern auch so was ähnliches gemacht um in einem object die
child-elemente zu sortieren.


 function sortObject($sort_key,$sort_direction = "ASC")
 {
  $this->attributes["sort_key"] = $sort_key;
  $this->attributes["sort_direction"] = $sort_direction;


  $new_object = $this;
  $new_object->children = array();
  foreach($this->children AS $temp_child_object_key => $temp_child_object)
  {
   $arr_Sort_to = array();
   foreach($temp_child_object->children AS $obj_profile_key => $obj_profile)
   {
    $arr_Sort_to[$obj_profile_key] =
$obj_profile->attributes[$this->attributes["sort_key"]];
   }
   natcasesort($arr_Sort_to);
   if ($sort_direction == "DESC")
   {
    $arr_Sort_to = array_reverse($arr_Sort_to);
   }
   $new_temp_child_object = $temp_child_object;
   $new_temp_child_object->children = array();
   foreach($arr_Sort_to AS $sort_to_key => $sort_to_value)
   {
    $new_temp_child_object->children[] =
$temp_child_object->children[$sort_to_key];
   }
   $new_object->children[$temp_child_object_key] = $new_temp_child_object;
  }
  $this = $new_object;

  return $this;
 }


vielleicht kannst du ja damit was anfangen.

greets

Dany



php::bar PHP Wiki   -   Listenarchive