我有一个数组:
$row = [
"serial_number" => "TDZL02616"
"id_machine" => "1720"
"action" => "go"
]
我还有一个多维数组,其中包含列及其顺序的定义(列'sequence'):
$colDef = [
[
'name' => 'serial_number',
'sequence' => 2,
'visible' => true,
],
[
'name' => 'id_machine',
'sequence' => 1,
'visible' => true,
],
[
'name' => 'action',
'sequence' => 3,
'visible' => true,
],
]
如何根据第二个数组($row
)和它的列'序列'轻松排序第一个数组($colDef
),我将得到结果:
$row = [
"id_machine" => "1720"
"serial_number" => "TDZL02616"
"action" => "go"
]
我不知道从哪里开始或如何解决它。
答案 0 :(得分:2)
试试这个。
$colDef2 = [];
foreach($colDef as $cd){
$colDef2[$cd['sequence']] = $cd['name'];
}
ksort($colDef2);
$row2 = [];
foreach($colDef2 as $i){
$row2[$i] = $row[$i];
}
注意使用PHP函数 ksort ,可以找到here。