2017-01-09 5 views
-1

cakePhpのfunction query()を使用しています。クエリのような配列何かが返されます:私は配列は、配列をマージします同じcate.dateを持っているかどうかを確認したい、今すぐ複数次元配列をマージする方法cakephp

array(
    (int) 0 => array(
     'cate' => array(
      'date' => '2016-12-05', 
     ), 
     'cate_detail' => array(
      'rel_data_category' => '11' 
     ), 
     'cate_item' => array(
      'price' => '150.000' 
     ) 
    ), 
    (int) 1 => array(
     'cate' => array(
      'date' => '2016-12-05', 
     ), 
     'cate_detail' => array(
      'rel_data_category' => '10' 
     ), 
     'cate_item' => array(
      'price' => '250.000' 
     ) 
    ), 
    (int) 2 => array(
     'cate' => array(
      'date' => '2016-12-06', 
     ), 
     'cate_detail' => array(
      'rel_data_category' => '10' 
     ), 
     'cate_item' => array(
      'price' => '250.000' 
     ) 
    ) 
) 

を(このケースでは、私の配列の要素0,1です)。次のような出力:

array(
    (int) 0 => array(
     'cate' => array(
      'date' => '2016-12-05', 
     ), 
     'cate_detail' => array(
      (int) 0 => array (
       'rel_data_category' => '11', 
       'price' => '150.000' 
      ), 
      (int) 1 => array(
       'rel_data_category' => '10', 
       'price' => '250.000' 
      ) 
     ) 
    ), 
    (int) 1 => array(
     'cate' => array(
      'date' => '2016-12-06', 
     ), 
     'cate_detail' => array(
      (int) 0 => array (
       'rel_data_category' => '10' 
       'price' => '250.000' 
      ) 
     ) 
    ) 
) 

助けてください!

+2

'cate_total'?どこからこのインデックスが突然あなたの望む出力に来るのですか?それはあなたの意見ではありません。また、希望の出力にいくつか間違った引用符のチェックとractify –

+0

ねえ、私は私の投稿を編集します! –

答えて

2

結果をループし、必要な形式のデータを含む新しい配列を作成する必要があります。 CakePHP Hash::extract() methodを使用して日付をインデックスにマップすると、日付のデータを組み合わせることができます。例えば

: -

// Get out all the dates available and then flip the array so that we have a map of dates to indexes 
$dates = Hash::extract($results, '{n}.cate.date'); 
$datesMap = array_flip($dates); 
// Define an empty array that we will store the new merged data in 
$data = []; 
// Loop through the query results and write to the $data array using the map of dates 
foreach ($results as $result) { 
    $key = $datesMap[$result['cate']['date']]; 
    $data[$key]['cate'] = $result['cate']; 
    $data[$key]['cate_detail'][] = [ 
     'rel_data_category' => $result['cate_detail']['rel_data_category'], 
     'price' => $result['cate_item']['price'] 
    ]; 
} 
+0

また、私の配列に 'item.code'があり、' if(item.code && cate.date)が同じ値を持っているかどうか 'チェックしたいと思います。 @drmonkeyninja –

+0

@longhoangvan私はあなたがこれを見たいとは思っていませんが、あなたの状況に対処するためにいくつかのif文を使って上の私のサンプルコードを開発することができます。 – drmonkeyninja

関連する問題