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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵