如何根据php中的特定键创建排序的多维数组

发布于 2024-12-15 07:15:43 字数 1963 浏览 0 评论 0原文

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

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
            )
        )
)

I have an array that I would like to make a multidimensional array based on a particular index in it.

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
        )

)

I want to get the following output

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
            )
        )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枕梦 2024-12-22 07:15:43

使用这个功能

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");

use this function

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");
瑶笙 2024-12-22 07:15:43

可能的解决方案:

  1. 获取所有日期,排序;

  2. 在循环中,查找与日期匹配的记录,将其所有内容添加到“索引”中的结果数组中;

Possible soln:

  1. Get all the dates, sort it;

  2. In a loop, find a record matching the date, add all contetns it to resulting array in '' index;

会傲 2024-12-22 07:15:43

尝试这个:

<?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
                )

        )

)

try this one:

<?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);

Got the following result:

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
                )

        )

)
咋地 2024-12-22 07:15:43

试试这个:

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;     
    }   
}

查看工作演示

try this:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文