Hi!!!

I would like to sort array below using the last array of a multi-dimensional array.

$list["access"][5] = "free"; //want to sort by numbers array 
$list["section"][2] = "soccer";
$list["type"][3] = "news";
$list["group"][1] = "sports";
$list["publication"][4] = "programmed";

I need your php skills to solve my problem, please.

Kindest Regards,

marcoBR

    asort sorts an associative array according to the value of each element.

    ksort sorts an associative array according to the key rather than the value

    asort($list) will sort by the numbers
    ksort($list) will put the array in alphabetical order

    Page 80, in the PHP, MYSQL book that I have. I looked in the index under sort.

    B

      ... didn't work with multi-dimensional array ... thanks anyway.

      I've tried using the functions usort() and uksort(), but i didn't know how to build a specific classification function for my case, maybe it's not possible.

      $list["access"][5] = "free"; //want to sort by numbers array 
      $list["section"][2] = "soccer";
      $list["type"][3] = "news";
      $list["group"][1] = "sports";
      $list["publication"][4] = "programmed";
      
      //asort($list); didn't work
      
      //ksort($list); didn't work
      
      //uksort($list, create_function('$a, $b', 'return strcmp($list[$a], $list[$b]);')); didn't work
      
      //array_multisort($list, SORT_NUMERIC, array_keys($list)); didn't work
      
      //Guru code here ... will work!
      
      foreach($list as $cap => $arr){
      	foreach($arr as $order => $field){
      		echo $order." - ".$cap."=>".$field."<br>";
      	}
      }
      

      Wanted result:

      1 - group=>sports
      2 - section=>soccer
      3 - type=>news
      4 - publication=>programmed
      5 - access=>free

      I'm going crazy ... help me please! 😕

        Write a Reply...