2017-08-25 20 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であるべきです。同様に1x2およびHandicapの第2レベルはHome, Draw, Away and Totalである必要があります。 の第2レベルは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] 
     } 
    } 
} 

最後に、$テンプレートが適切なデータで満たされています。

+0

良いアイデアのようです。私はこれを試してみる。 –

+0

いくつかのサンプルコードを追加しましたが、テストされていませんでした。確認できますか? – Fiber

+0

私はそれらを試してみましょう。 –

関連する問題