如何合并多维数组cakephp
我只是使用cakePhp的函数query()。查询将返回类似以下内容的数组:如何合并多维数组cakephp,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,我只是使用cakePhp的函数query()。查询将返回类似以下内容的数组: array( (int) 0 => array( 'cate' => array( 'date' => '2016-12-05', ), 'cate_detail' => array( 'rel_data_category' => '11' ), 'cat
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'
)
)
)
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'
)
)
)
)
现在,我想检查数组是否具有相同的cate.date
将合并数组(在本例中是数组的元素0,1)。输出如下内容:
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'
)
)
)
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'
)
)
)
)
请帮忙 您将需要遍历结果,并使用所需格式的数据构建一个新数组。可以使用将日期映射到索引,以便可以组合日期的数据 例如:-
// 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']
];
}
cate_total
?这个指数从哪里突然出现在你想要的输出中?它不在你的输入中。另外,希望输出有一些错位的报价检查和PractifyYep,我会编辑我的文章!另外,如果我的数组有item.code
,我想检查是否(item.code&&cate.date)有相同的值
。有没有办法让我检查一下@drmonkeyninja@longhoangvan我不确定您希望它看起来如何,但是您应该能够使用一些if语句来开发我上面的示例代码,也许可以处理您的情况。