如何将多维数组cakephp与两个键

时间:2017-01-10 03:22:12

标签: php cakephp cakephp-2.0

我只使用cakePhp的函数query()。该查询将返回如下数组:

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'
        )
    )
)

请帮忙!

1 个答案:

答案 0 :(得分: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