dony39517
dony39517
2014-09-14 06:24

如何使用kso​​rt使用PHP对数组中的多个数组进行排序? [重复]

This question already has an answer here:

I've trawled a lot of questions and php manual, but I can't find a way to get sort this data in a graceful way. There may not be, but I'll settle for non-graceful.

At the moment I have a page that builds 4 arrays with data from post. The number of keys changes depending on the input;

// Grab the Tasks
$arraytask = $_POST["task"];

// Grab the Relusers
$arrayreluser = $_POST["reluser"];

// Grab the Usernames
$arrayuser = $_POST["user"];

// Grab the License Types
$arraylicense = $_POST["license"];

$result = array();
foreach( $arraytask as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arrayreluser as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arrayuser as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arraylicense as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
ksort($result); // I know this does nothing, I was hoping it would recursively sort or something

At the moment, the output on an example submission looks like (and sorry for the long formatting):

print_r($result);
Array (
         [0] => Array (
         [key] => 0
         [value] => 123 ) 
         [1] => Array ( 
         [key] => 1 
         [value] => 456 ) 
         [2] => Array ( 
         [key] => 2 
         [value] => 789 ) 
         [3] => Array ( 
         [key] => 0 
         [value] => qwe ) 
         [4] => Array ( 
         [key] => 1 
         [value] => rty ) 
         [5] => Array ( 
         [key] => 2 
         [value] => uio ) 
         [6] => Array ( 
         [key] => 0 
         [value] => asd ) 
         [7] => Array ( 
         [key] => 1 
         [value] => fgh ) 
         [8] => Array ( 
         [key] => 2 
         [value] => jkl ) 
         [9] => Array ( 
         [key] => 0 
         [value] => license 1 ) 
         [10] => Array ( 
         [key] => 1 
         [value] => license 2 ) 
         [11] => Array ( 
         [key] => 2 
         [value] => license 3 )
 )

However I want the output to be like

print_r($result);
 Array (
         [0] => Array (
         [key] => 0
         [value] => 123 ) 
         [3] => Array ( 
         [key] => 0 
         [value] => qwe ) 
         [6] => Array ( 
         [key] => 0 
         [value] => asd ) 
         [9] => Array ( 
         [key] => 0 
         [value] => license 1 )          
         [1] => Array ( 
         [key] => 1 
         [value] => 456 ) 
         [4] => Array ( 
         [key] => 1 
         [value] => rty ) 
         [7] => Array ( 
         [key] => 1 
         [value] => fgh ) 
         [10] => Array ( 
         [key] => 1 
         [value] => license 2 )          
         [2] => Array ( 
         [key] => 2 
         [value] => 789 ) 
         [5] => Array ( 
         [key] => 2 
         [value] => uio ) 
         [8] => Array ( 
         [key] => 2 
         [value] => jkl ) 
         [11] => Array ( 
         [key] => 2 
         [value] => license 3 )
 )

I know I'm sorting Arrays by their keys... I just can't think of a better way to sort this data.

At the moment I've looked at array_merge() which seems to overwrite duplicate keys, and I've tried a few variations of foreach loops which have just ended in tears for everyone involved.

An alternative way to ask this question would be "If I can't sort these arrays by the keys within them, can I merge my 4 arrays so that the values of each array compile in to a single array, based off key?"

An acceptable (seemingly more graceful) output would also be

 Array (
         [0] => Array (
         [key] => 0
         [value] => 123, qwe, asd, license 1 ) 
         [1] => Array ( 
         [key] => 1 
         [value] => 456, rty, fgh, license 2 ) 
         [2] => Array ( 
         [key] => 2 
         [value] => 789, uio, jkl, license 3 ) 
         )

I'm just not sure I can append values to keys in an array, if I do not explicitly know how many keys there are. Postscript: if there are typos here, that's because this is the example cut down from the actual code for clarity, and I'm sorry. My issue isn't typos.

::SOLUTION::

Thanks to vstm, this worked for combining multiple arrays into a more useful array data;

$result = array();
foreach($arraytask as $key => $val) {
   $result[] = array(
        'key' => $key,
        'task' => $arraytask[$key],
        'reluser' => $arrayreluser[$key],
        'user' => $arrayuser[$key],
        'license' => $arraylicense[$key],
        'value' => implode(', ', array(
            $arraytask[$key],
            $arrayreluser[$key],
            $arrayuser[$key],
            $arraylicense[$key],
        ))
    ); 
}

Shows the output as

 Array ( 
 [0] => Array ( 
 [key] => 0 
 [task] => 123 
 [reluser] => qwe 
 [user] => asd 
 [license] => license 1 
 [value] => 123, qwe, asd, license 1 ) 
 [1] => Array ( 
 [key] => 1 
 [task] => 456 
 [reluser] => rty 
 [user] => fgh 
 [license] => license 2 
 [value] => 456, rty, fgh, license 2 ) )
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpka7974 dpka7974 7年前

    Well it seems that your input data is already given in a way that would make sorting useless. Try it this way:

    // Grab the Tasks
    $arraytask = $_POST["task"];
    
    // Grab the Relusers
    $arrayreluser = $_POST["reluser"];
    
    // Grab the Usernames
    $arrayuser = $_POST["user"];
    
    // Grab the License Types
    $arraylicense = $_POST["license"];
    
    $result = array();
    foreach($arraytask as $key => $val) {
       $result[] = array(
            'key' => $key,
            'task' => $arraytask[$key],
            'reluser' => $arrayreluser[$key],
            'user' => $arrayuser[$key],
            'license' => $arraylicense[$key],
            'value' => implode(', ', array(
                $arraytask[$key],
                $arrayreluser[$key],
                $arrayuser[$key],
                $arraylicense[$key],
            ))
        ); 
    }
    

    Now you have your "seemingly graceful" output, plus access to all the fields which you might need for working with your data. No need for sorting.

    点赞 评论 复制链接分享
  • douwen5066 douwen5066 7年前

    Try by usort(). Example here...

    function sortByValue($a, $b) {
        return $a['key'] - $b['key'];
    }
    usort($arr, 'sortByValue');
    
    print '<pre>';
    print_r($arr);
    
    点赞 评论 复制链接分享