2017-01-10 68 views
0

我只是使用cakePhp的函數query()。該查詢將返回數組是這樣的:如何使用兩個鍵合併多維數組cakephp

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

現在,我要檢查,如果數組具有相同的cate.dateitem.code將合併陣列(在這種情況下我的數組的元素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' 
      ) 
     ), 
     'item' => array(
      'code' => '1' 
     ) 
    ), 
    (int) 1 => array(
     'cate' => array(
      'date' => '2016-12-06', 
     ), 
     'cate_detail' => array(
      (int) 0 => array (
       'rel_data_category' => '10' 
       'price' => '250.000' 
      ) 
     ), 
     'item' => array(
      'code' => '2' 
     ) 
    ), 
    (int) 2 => array(
     'cate' => array(
      'date' => '2016-12-06', 
     ), 
     'cate_detail' => array(
      (int) 0 => array (
       'rel_data_category' => '10' 
       'price' => '250.000' 
      ) 
     ), 
     'item' => array(
      'code' => '1' 
     ) 
    ) 
) 

請幫忙!

+0

你不使用CakePHP查詢構建器爲這個? – Beginner

+0

是的,我只是使用'$ result = $ this-> query($ query);' –

+0

你可以提供你正在使用的完整查詢,因爲你想要的數組可以通過查詢本身使用'hasmany'完成 –

回答

1

要做到這一點

首先聲明這以後你會存儲新的合併數組數據

$filter_data = array(); 

一個變量,然後循環現有的陣列

foreach($items as $item) { 

    // do somethind 

然後創建該檢查功能物品相同datecode尚未存在於新陣列中

// checker 
    if (!checkExist($item['cate']['date'], $item['item']['code'])) { 

創建臨時數據的變量,將追加到新陣列後

$data = array(
     'cate' => $item['cate'], 
     'item' => $item['item'] 
    ); 

然後重新創建循環,這將追加所有索引項cate_detailcate_item誰具有相同的 datecode從父環

} 
} 

所以,你的代碼會是這樣

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

$filter_data = array(); 

foreach($items as $item) { 
    if (!checkExist($item['cate']['date'], $item['item']['code'])) { 
    $data = array(
     'cate' => $item['cate'], 
     'item' => $item['item'] 
    ); 
    foreach($items as $detail) { 
     if ($detail['cate']['date'] == $item['cate']['date'] && 
      $detail['item']['code'] == $item['item']['code']) { 
     $data['cate_detail'][] = array(
      'rel_data_category' => $detail['cate_detail']['rel_data_category'], 
      'price' => $detail['cate_item']['price'] 
     ); 
     } 
    } 
    $filter_data[] = $data; 
    } 
} 

function checkExist($date, $code){ 
    global $filter_data; 
    foreach($filter_data as $data) { 
    if ($data['cate']['date'] == $date && 
     $data['item']['code'] == $code) { 
     return true; 
    } 
    } 
    return false; 
} 

pr($filter_data); 

Demo

OR

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

$filter_data = array(); 
$exists = array(); 

foreach($items as $item) { 
    $index_key = $item['cate']['date'].'-'.$item['item']['code']; 
    if (!isset($exists[$index_key])) { 
     $exists[$index_key] = 1; 
     $data = array(
      'cate' => $item['cate'], 
      'item' => $item['item'], 
      'cate_detail' => getItems($item['cate']['date'], $item['item']['code']) 
     ); 
     $filter_data[] = $data; 
    } 
} 

function getItems($date, $code) { 
    global $items; 
    $data = array(); 
    foreach($items as $detail) { 
     if ($detail['cate']['date'] == $date && $detail['item']['code'] == $code) { 
      $data[] = array(
      'rel_data_category' => $detail['cate_detail']['rel_data_category'], 
      'price' => $detail['cate_item']['price'] 
     ); 
     } 
    } 
    return $data; 
} 

pr($filter_data); 

Demo

+0

感謝幫幫我。這很有幫助! –