dony39517 2014-09-14 06:24
浏览 35
已采纳

如何使用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 2014-09-14 06:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿