4

Consider following array

    $details = array( 
 array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'),
 array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n'),
 array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'),
 array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
 array('lname'=>'E', 'fname'=>'T','membkey'=>700,'head'=>'n')
    );

Here I would like to sort with head and membkey. Top element of same membkey element should have 'head=y' and echoed as,

    $details = array( 
 array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'),
 array('lname'=>'E', 'fname'=>'T','membkey'=>700,'head'=>'n'),
 array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'),
 array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
 array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n')
    );

I tried it as follows

    function orderbymemberKey( $a, $b ){
    if ( $a[membkey] == $b[membkey] )
        return 0;
    return($a[membkey] < $b[membkey] )? -1 :1;
 }
 usort( $details, orderbymemberKey );
and it successfully order by membkey.

Any suggestions please.

2
  • you need to be more clear. your question is a mess. Commented Aug 31, 2010 at 5:03
  • added a generic "sort_by" function to my answer which could make things easier for you Commented Aug 31, 2010 at 7:17

4 Answers 4

10

You're half way there (though you were sorting backwards for membkey based on your example):

function order_by_member_key($a, $b)
{
  if ($a['membkey'] == $b['membkey'])
  {
    // membkey is the same, sort by head
    if ($a['head'] == $b['head']) return 0;
    return $a['head'] == 'y' ? -1 : 1;
  }

  // sort the higher membkey first:
  return $a['membkey'] < $b['membkey'] ? 1 : -1;
}
usort($details, "order_by_member_key");
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you so much! everything there is exactly what I needed!
0

Is this array being pulled from a database? Because, if so, you should be able to make use of ORDER BY clauses to clean it up outside of php.

1 Comment

Thank you for all of your contribution
0
<?php

$membkey = array();
$head = array();
foreach ($details as $key => $row) {
    $membkey[$key]  = $row['membkey'];
    $head[$key] = $row['head'];
}

array_multisort($membkey, SORT_DESC, $head, SORT_DESC, $details);

print_r($details);

Or, an even more generic solution:

function sort_by($array) {
    $arguments = func_get_args();
    $array = array_pop($arguments);
    $variables = array();
    foreach ($arguments as $index => $key) {
        $variables[] = '$arguments['.$index.']';
        if ($index % 2 == 0) {
            $arguments[$index] = array();
            foreach ($array as $row) $arguments[$index][] = $row[$key];
        }
    }
    // call_user_func_array will not work in this case
    eval('array_multisort('.implode(', ', $variables).', $array);');
    return $array;
}

print_r(sort_by('membkey', SORT_DESC, 'head', SORT_DESC, $details));

2 Comments

Why don't you use call_user_func() instead of eval()? This code might expose a security hole.
@Yorirou call_user_func_array doesn't work in this case because array_multisort expects the last argument to be a reference to the array to sort
-2

Ugly but someone wrote a function on php.net: http://php.net/manual/en/function.sort.php

<?php

$array[0]['name']  = 'Chris';
$array[0]['phone'] = '3971095';
$array[0]['year']  = '1978';
$array[0]['address'] = 'Street 1';

$array[1]['name']  = 'Breanne';
$array[1]['phone'] = '3766350';
$array[1]['year']  = '1990';
$array[1]['address'] = 'Street 2';

$array[2]['name']  = 'Dusty';
$array[2]['phone'] = '1541120';
$array[2]['year']  = '1982';
$array[2]['address'] = 'Street 3';

function multisort($array, $sort_by, $key1, $key2=NULL, $key3=NULL, $key4=NULL, $key5=NULL, $key6=NULL){
    // sort by ?
    foreach ($array as $pos =>  $val)
        $tmp_array[$pos] = $val[$sort_by];
    asort($tmp_array);

    // display however you want
    foreach ($tmp_array as $pos =>  $val){
        $return_array[$pos][$sort_by] = $array[$pos][$sort_by];
        $return_array[$pos][$key1] = $array[$pos][$key1];
        if (isset($key2)){
            $return_array[$pos][$key2] = $array[$pos][$key2];
            }
        if (isset($key3)){
            $return_array[$pos][$key3] = $array[$pos][$key3];
            }
        if (isset($key4)){
            $return_array[$pos][$key4] = $array[$pos][$key4];
            }
        if (isset($key5)){
            $return_array[$pos][$key5] = $array[$pos][$key5];
            }
        if (isset($key6)){
            $return_array[$pos][$key6] = $array[$pos][$key6];
            }
        }
    return $return_array;
    }

//usage (only enter the keys you want sorted):

$sorted = multisort($array,'year','name','phone','address');
print_r($sorted);

//output:
Array ( [0] => Array ( [year] => 1978 [name] => Chris [phone] => 3971095 [address] => Street 1 ) [2] => Array ( [year] => 1982 [name] => Dusty [phone] => 1541120 [address] => Street 3 ) [1] => Array ( [year] => 1990 [name] => Breanne [phone] => 3766350 [address] => Street 2 ) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.