我有以下动态创建的数组。
Array
(
[1x2] => Array
(
[AWAY] => Array
(
[PL] => 1.6
[Yield] => 19.4
[YieldLS] => 19.4
[Picks] => 8
[Price] => 14.97
[Stake] => 8
[AvgStake] => 1
[AvgOdd] => 1.87
)
[HOME] => Array
(
[PL] => 6.9
[Yield] => 34.7
[YieldLS] => 34.7
[Picks] => 20
[Price] => 38.25
[Stake] => 20
[AvgStake] => 1
[AvgOdd] => 1.91
)
[DRAW] => Array
(
[PL] => 2.4
[Yield] => 240
[YieldLS] => 240
[Picks] => 1
[Price] => 3.4
[Stake] => 1
[AvgStake] => 1
[AvgOdd] => 3.4
)
[TOTAL] => Array
(
[PL] => 10.9
[Yield] => 294.1
[YieldLS] => 294.1
[Picks] => 29
[Price] => 56.62
[Stake] => 29
[AvgStake] => 3
[AvgOdd] => 7.18
)
)
[Asian Handicap] => Array
(
[HOME] => Array
(
[PL] => 41.9
[Yield] => 9.3
[YieldLS] => 9.3
[Picks] => 448
[Price] => 885.79
[Stake] => 448
[AvgStake] => 1
[AvgOdd] => 1.98
)
[AWAY] => Array
(
[PL] => 31.3
[Yield] => 7.1
[YieldLS] => 7.1
[Picks] => 439
[Price] => 877.33
[Stake] => 439
[AvgStake] => 1
[AvgOdd] => 2
)
[TOTAL] => Array
(
[PL] => 73.2
[Yield] => 16.4
[YieldLS] => 16.4
[Picks] => 887
[Price] => 1763.12
[Stake] => 887
[AvgStake] => 2
[AvgOdd] => 3.98
)
)
[Over/Under] => Array
(
[OVER] => Array
(
[PL] => 14.7
[Yield] => 10.1
[YieldLS] => 10.1
[Picks] => 145
[Price] => 281.45
[Stake] => 145
[AvgStake] => 1
[AvgOdd] => 1.94
)
[UNDER] => Array
(
[PL] => 14.1
[Yield] => 19.5
[YieldLS] => 19.5
[Picks] => 72
[Price] => 140.94
[Stake] => 72
[AvgStake] => 1
[AvgOdd] => 1.96
)
[TOTAL] => Array
(
[PL] => 28.8
[Yield] => 29.6
[YieldLS] => 29.6
[Picks] => 217
[Price] => 422.39
[Stake] => 217
[AvgStake] => 2
[AvgOdd] => 3.9
)
)
)
我想要的是根据具体要求对其进行排序。例如,我需要输出为
Array
(
[1x2] => Array
(
[HOME] => Array
(
[PL] => 6.9
[Yield] => 34.7
[YieldLS] => 34.7
[Picks] => 20
[Price] => 38.25
[Stake] => 20
[AvgStake] => 1
[AvgOdd] => 1.91
)
[DRAW] => Array
(
[PL] => 2.4
[Yield] => 240
[YieldLS] => 240
[Picks] => 1
[Price] => 3.4
[Stake] => 1
[AvgStake] => 1
[AvgOdd] => 3.4
)
[AWAY] => Array
(
[PL] => 1.6
[Yield] => 19.4
[YieldLS] => 19.4
[Picks] => 8
[Price] => 14.97
[Stake] => 8
[AvgStake] => 1
[AvgOdd] => 1.87
)
[TOTAL] => Array
(
[PL] => 10.9
[Yield] => 294.1
[YieldLS] => 294.1
[Picks] => 29
[Price] => 56.62
[Stake] => 29
[AvgStake] => 3
[AvgOdd] => 7.18
)
)
[Asian Handicap] => Array
(
[HOME] => Array
(
[PL] => 41.9
[Yield] => 9.3
[YieldLS] => 9.3
[Picks] => 448
[Price] => 885.79
[Stake] => 448
[AvgStake] => 1
[AvgOdd] => 1.98
)
[AWAY] => Array
(
[PL] => 31.3
[Yield] => 7.1
[YieldLS] => 7.1
[Picks] => 439
[Price] => 877.33
[Stake] => 439
[AvgStake] => 1
[AvgOdd] => 2
)
[TOTAL] => Array
(
[PL] => 73.2
[Yield] => 16.4
[YieldLS] => 16.4
[Picks] => 887
[Price] => 1763.12
[Stake] => 887
[AvgStake] => 2
[AvgOdd] => 3.98
)
)
[Over/Under] => Array
(
[OVER] => Array
(
[PL] => 14.7
[Yield] => 10.1
[YieldLS] => 10.1
[Picks] => 145
[Price] => 281.45
[Stake] => 145
[AvgStake] => 1
[AvgOdd] => 1.94
)
[UNDER] => Array
(
[PL] => 14.1
[Yield] => 19.5
[YieldLS] => 19.5
[Picks] => 72
[Price] => 140.94
[Stake] => 72
[AvgStake] => 1
[AvgOdd] => 1.96
)
[TOTAL] => Array
(
[PL] => 28.8
[Yield] => 29.6
[YieldLS] => 29.6
[Picks] => 217
[Price] => 422.39
[Stake] => 217
[AvgStake] => 2
[AvgOdd] => 3.9
)
)
)
在第一级1x2
应该是第一项,然后是Asian Handicap
,最后是Over/Under
。同样,1x2
和Handicap
第二级应为Home, Draw, Away and Total
。在Over/Under
第二级Over then Under and then Total
。对于缺少的任何项目/元素,我需要添加空记录/数组。 elements
和keys
的数字将始终固定。
缺少案例可以像这样
Array
(
[1x2] => Array
(
[HOME] => Array
(
[PL] => ''
[Yield] => ''
[YieldLS] => ''
[Picks] => ''
[Price] => ''
[Stake] => ''
[AvgStake] => ''
[AvgOdd] => ''
)
[DRAW] => Array
(
[PL] => 2.4
[Yield] => 240
[YieldLS] => 240
[Picks] => 1
[Price] => 3.4
[Stake] => 1
[AvgStake] => 1
[AvgOdd] => 3.4
)
[AWAY] => Array
(
[PL] => 1.6
[Yield] => 19.4
[YieldLS] => 19.4
[Picks] => 8
[Price] => 14.97
[Stake] => 8
[AvgStake] => 1
[AvgOdd] => 1.87
)
[TOTAL] => Array
(
[PL] => 10.9
[Yield] => 294.1
[YieldLS] => 294.1
[Picks] => 29
[Price] => 56.62
[Stake] => 29
[AvgStake] => 3
[AvgOdd] => 7.18
)
)
[Asian Handicap] => Array
(
[HOME] => Array
(
[PL] => 41.9
[Yield] => 9.3
[YieldLS] => 9.3
[Picks] => 448
[Price] => 885.79
[Stake] => 448
[AvgStake] => 1
[AvgOdd] => 1.98
)
[AWAY] => Array
(
[PL] => ''
[Yield] => ''
[YieldLS] => ''
[Picks] => ''
[Price] => ''
[Stake] => ''
[AvgStake] => ''
[AvgOdd] => ''
)
[TOTAL] => Array
(
[PL] => 73.2
[Yield] => 16.4
[YieldLS] => 16.4
[Picks] => 887
[Price] => 1763.12
[Stake] => 887
[AvgStake] => 2
[AvgOdd] => 3.98
)
)
[Over/Under] => Array
(
[OVER] => Array
(
[PL] => 14.7
[Yield] => 10.1
[YieldLS] => 10.1
[Picks] => 145
[Price] => 281.45
[Stake] => 145
[AvgStake] => 1
[AvgOdd] => 1.94
)
[UNDER] => Array
(
[PL] => 14.1
[Yield] => 19.5
[YieldLS] => 19.5
[Picks] => 72
[Price] => 140.94
[Stake] => 72
[AvgStake] => 1
[AvgOdd] => 1.96
)
[TOTAL] => Array
(
[PL] => 28.8
[Yield] => 29.6
[YieldLS] => 29.6
[Picks] => 217
[Price] => 422.39
[Stake] => 217
[AvgStake] => 2
[AvgOdd] => 3.9
)
)
)
我尝试过使用单独的数组进行排序,但是在复杂的情况下将错误数组和排序的逻辑放在一起。任何帮助将受到高度赞赏。
答案 0 :(得分:1)
我会以正确的顺序创建一个空记录的模板数组,然后迭代它并检查源数组是否具有相同的关联元素。
如果它存在且不为空,则使用源覆盖模板项。 如果模板符合您的需求,那么最后您将拥有一个以正确顺序填充数据的模板,如果您有空白,则填充模板的空数据。
<?php
$sourceWrongOrder = [];
$emptyRecord = [
'PL' => '',
'Yield' => '',
'YieldLS' => '',
'Picks' => '',
'Price' => '',
'Stake' => '',
'AvgStake' => '',
'AvgOdd' => '',
];
$template = [
'1x2' =>
[
'HOME' => $emptyRecord,
'AWAY' => $emptyRecord,
'TOTAL' => $emptyRecord,
],
'Asian Handicap' =>
[
'HOME' => $emptyRecord,
'AWAY' => $emptyRecord,
'TOTAL' => $emptyRecord,
],
'Over/Under' =>
[
'OVER' => $emptyRecord,
'UNDER' => $emptyRecord,
'TOTAL' => $emptyRecord,
]
];
foreach ($template as $name => &$items)
{
foreach ($items as $title => &$item)
{
if (isset($sourceWrongOrder[$name][$title]))
{
$item = $sourceWrongOrder[$name][$title]
}
}
}
最后,您的$ template正好填写了正确的数据。