Jump to content

[SOLVED] advanced array sorting


surion

Recommended Posts

hi, i got an array containing arrays, wich should get sorted one way or another. this is what the array looks like (var_dump of it)

 

array(234) {
  [0]=>
  array(1) {
    [746]=>
    string(15) "X some alphanumeric data"
  }
  [1]=>
  array(1) {
    [747]=>
    string(15) "F some alphanumeric data"
  }
  [2]=>
  array(1) {
    [748]=>
    string(17) "E some alphanumeric data"
  }
  [3]=>
  array(1) {
    [749]=>
    string(11) "ER some alphanumeric data"
  }
  [4]=>
  array(1) {
    [750]=>
    string(10) "some alphanumeric data"
  }
  [5]=>
  array(1) {
    [751]=>
    string(22) "lsqmdkd some alphanumeric data"
  }
}

 

the big array should have the "smaller arrays" in alphabetical order of the string values inside the smaller arrays, is there a possibility to fix this easy? by the way, it is of great importance that the key and the string of the smaller arrays stay together

 

i can't sort the data before it goes into the array, because i don't put it in there myself. i just get the array as it is

 

*edit* dunno if i explained good enough, but this is what it should look like after the sorting:

 

array(234) {
  [0]=>
  array(1) {
    [748]=>
    string(17) "E some alphanumeric data"
  }
  [1]=>
  array(1) {
    [749]=>
    string(11) "ER some alphanumeric data"
  }
  [2]=>
  array(1) {
    [747]=>
    string(15) "F some alphanumeric data"
  }
  [3]=>
  array(1) {
    [751]=>
    string(22) "lsqmdkd some alphanumeric data"
  }
  [4]=>
  array(1) {
    [750]=>
    string(10) "some alphanumeric data"
  }
  [5]=>
  array(1) {
    [746]=>
    string(15) "X some alphanumeric data"
  }
}

Link to comment
Share on other sites

try[code[<?php
function my_comp($a, $b){
foreach ($a as $v) $x = strtolower($v);
foreach ($b as $v) $y = strtolower($v);
if ($x > $y) return 1;
if ($x == $y) return 0;
return -1; 
}

$a = array(array(746=>"X some alphanumeric data"),
	   array(747=>"F some alphanumeric data"),
	   array(748=>"E some alphanumeric data"),
	   array(749=>"ER some alphanumeric data"),
	   array(750=>"some alphanumeric data"),
	   array(751=>"lsqmdkd some alphanumeric data")
  );
usort($a, 'my_comp');
print_r($a);
?>

Link to comment
Share on other sites

  • 1 month later...
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.