我有一个这样的数组:
array(
[0] => array(
'date' => '2017-12-20',
'name' => 'test A',
'kwa' => 'kwa1, kwa2'
),
[1] => array(
'date' => '2017-12-20',
'name' => 'test B',
'kwa' => ''
),
[2] => array(
'date' => '2017-12-21',
'name' => 'test C',
'kwa' => 'kwa1'
),
[3] => array(
'date' => '2017-12-22',
'name' => 'test D',
'kwa' => ''
),
[4] => array(
'date' => '2017-12-22',
'name' => 'test E',
'kwa' => ''
),
)
我希望组中的所有元素都在同一天进入数组。这样的事情:
array(
['2017-12-20'] => array( '0', '1'),
['2017-12-21'] => array( '2' ),
['2017-12-22'] => array( '3', '4' ),
)
PHP有什么功能可以做到这一点?或者有人知道最好的解决方案?谢谢!!!
答案 0 :(得分:2)
这是你的解决方案......
输入
<?php
$array = array(
array(
'date' => '2017-12-20',
'name' => 'test A',
'kwa' => 'kwa1, kwa2'
),array(
'date' => '2017-12-20',
'name' => 'test B',
'kwa' => ''
),array(
'date' => '2017-12-21',
'name' => 'test C',
'kwa' => 'kwa1'
),array(
'date' => '2017-12-22',
'name' => 'test D',
'kwa' => ''
),array(
'date' => '2017-12-22',
'name' => 'test E',
'kwa' => ''
),
);
<强>解决方案强>
$new = array();
foreach($array as $r){
$new[$r['date']][] = array('name'=>$r['name'],'kwa' => $r['kwa']);
}
echo "<pre>";print_r($new);
?>
输出
Array
(
[2017-12-20] => Array
(
[0] => Array
(
[name] => test A
[kwa] => kwa1, kwa2
)
[1] => Array
(
[name] => test B
[kwa] =>
)
)
[2017-12-21] => Array
(
[0] => Array
(
[name] => test C
[kwa] => kwa1
)
)
[2017-12-22] => Array
(
[0] => Array
(
[name] => test D
[kwa] =>
)
[1] => Array
(
[name] => test E
[kwa] =>
)
)
)
答案 1 :(得分:2)
$arr = array();
foreach($array1 as $key => $item)
{
$arr[$item['date']][$key] = $key;
}
echo "<pre>";
print_r($arr);
答案 2 :(得分:1)
我会使用以下内容:
Try the below code
getAllByQuery: function (query, callback) {
this
.find(query, {campaignId: 1})
.populate({path: 'campaignId',
model: 'campaign',
select: {
'campaignDetail': 0,
'area': 0,
},
populate: [{
path: 'ad_Id',
model: 'Ad',
select: { 'Level': 1}
},
{
path: 'categories',
model: 'Category',
select: { '_id': 0}
}]
})
.exec(callback);
}