我只使用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.date
和item.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 :(得分:1)
要做到这一点
首先声明一个变量,稍后您将存储新的合并数组数据
$filter_data = array();
然后循环现有的数组
foreach($items as $item) {
// do somethind
然后创建一个函数,检查新数组中是否存在相同date
和code
的项目
// checker
if (!checkExist($item['cate']['date'], $item['item']['code'])) {
为稍后将附加到新数组的临时数据创建一个变量
$data = array(
'cate' => $item['cate'],
'item' => $item['item']
);
然后再次创建循环,它将追加具有相同内容的所有索引项cate_detail
和cate_item
来自父循环的date
和code
}
}
所以你的代码就像这样
$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);
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);