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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器