2017-08-25 50 views
-1

我有以下动态创建的数组。排序具有特定要求的关联数组

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。同样在1x2Handicap二级应该是Home, Draw, Away and Total。在Over/Under第二级是Over then Under and then Total。对于任何项目/元素缺失,我需要添加空记录/数组。数字elementskeys将始终是固定的。

缺失情况下,可以是这样的

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 
      ) 

    ) 
) 

我已经使用单独的阵列用于分选,但把缺少阵列的逻辑和排序立刻在复杂作出尝试。任何帮助将不胜感激。

回答

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成为正确的顺序填充正确的数据。

+0

听起来很不错。我会试试这个。 –

+0

增加了一些示例代码,但没有经过测试,您可以验证它吗? – Fiber

+0

让我试试他们。 –