首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php中创建基于特定键的排序多维数组

如何在php中创建基于特定键的排序多维数组
EN

Stack Overflow用户
提问于 2011-11-15 14:37:32
回答 4查看 159关注 0票数 0

我有一个数组,我想根据其中的特定索引创建一个多维数组。

代码语言:javascript
复制
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
        )

)

我想要获得以下输出

代码语言:javascript
复制
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
            )
        )
)
EN

回答 4

Stack Overflow用户

发布于 2011-11-15 14:45:13

使用此函数

代码语言:javascript
复制
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");
票数 2
EN

Stack Overflow用户

发布于 2011-11-15 14:45:54

可能的解决方案:

  1. 获取所有日期,对其进行排序;
  2. 在循环中,找到与日期匹配的记录,将所有内容添加到'‘index中的结果数组;
票数 0
EN

Stack Overflow用户

发布于 2011-11-15 14:50:12

试试这个:

代码语言:javascript
复制
<?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);

得到的结果如下:

代码语言:javascript
复制
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
                )

        )

)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8132417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档