我有一个数组,我想根据其中的特定索引创建一个多维数组。
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
)
)
)发布于 2011-11-15 14:45:13
使用此函数
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");发布于 2011-11-15 14:45:54
可能的解决方案:
发布于 2011-11-15 14:50:12
试试这个:
<?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
)
)
)https://stackoverflow.com/questions/8132417
复制相似问题