我有一个数组,我想基于其中的特定索引创建一个多维数组。
Array
(
[0] => Array
(
[notecata] => Tele Call
[user_id] => 1
[note_key] => 4977f48e
[note_title] => Urgent Call to Soorya
[note_description] => want to discuss about the work
[added_on] => 15-11-11
)
[1] => Array
(
[notecata] => Set PlaceMent Drive
[user_id] => 1
[note_key] => b8b25bd8
[note_title] => Want to collect biodata from Students
[note_description] => Soorya must do this very well
[added_on] => 15-11-11
)
[2] => Array
(
[notecata] => Conference
[user_id] => 1
[note_key] => 3cdb4886
[note_title] => Sunday Meeting
[note_description] => About new courses
[added_on] => 08-11-11
)
)
我想获得以下输出
Array
(
[15-11-11] => Array
(
[0] => Array(
[notecata] => Tele Call
[user_id] => 1
[note_key] => 4977f48e
[note_title] => Urgent Call to Soorya
[note_description] => want to discuss about the work
)
[1] => Array(
[notecata] => Set PlaceMent Drive
[user_id] => 1
[note_key] => b8b25bd8
[note_title] => Want to collect biodata from Students
[note_description] => Soorya must do this very well
)
)
[8-11-11] => Array
(
[0] => Array(
[notecata] => Conference
[user_id] => 1
[note_key] => 3cdb4886
[note_title] => Sunday Meeting
[note_description] => About new courses
)
)
)
答案 0 :(得分:2)
使用此功能
function change_array_keys($array, $key) {
$return = array();
foreach ($array as $a) {
$return[$a[$key]][] = $a;
}
return $return;
}
$newArray = change_array_keys($array, "added_on");
答案 1 :(得分:0)
可能的解决方案:
获取所有日期,排序;
在一个循环中,找到一个与日期匹配的记录,在''索引中添加所有对生成数组的争议;
答案 2 :(得分:0)
试试这个:
<?php
header('Content-Type: Text/Plain');
$array = array();
$array[] = array('note' => 'asdf', 'added_on' => '15-11-11');
$array[] = array('note' => 'abcd', 'added_on' => '15-11-11');
$array[] = array('note' => 'qwer', 'added_on' => '15-11-11');
$array[] = array('note' => 'zxcv', 'added_on' => '08-11-11');
print_r($array);
$sorted = array();
foreach( $array as $each)
{
$current_each_date = $each['added_on'];
unset($each['added_on']);
$sorted[ $current_each_date ][] = $each;
}
print_r($sorted);
得到以下结果:
Array
(
[0] => Array
(
[note] => asdf
[added_on] => 15-11-11
)
[1] => Array
(
[note] => abcd
[added_on] => 15-11-11
)
[2] => Array
(
[note] => qwer
[added_on] => 15-11-11
)
[3] => Array
(
[note] => zxcv
[added_on] => 08-11-11
)
)
Array
(
[15-11-11] => Array
(
[0] => Array
(
[note] => asdf
)
[1] => Array
(
[note] => abcd
)
[2] => Array
(
[note] => qwer
)
)
[08-11-11] => Array
(
[0] => Array
(
[note] => zxcv
)
)
)
答案 3 :(得分:0)
试试这个:
foreach ($input as $k=>$v)
{
$array_key = $v['added_on'];
unset($v['added_on']);
if( array_key_exists($array_key,$output) )
{
array_push($output[$array_key],$v);
}
else {
$output[$array_key][] = $v;
}
}
<强> SEE WORKING DEMO 强>