簡體   English   中英

如何根據php中的特定鍵創建一個排序的多維數組

[英]How to make a sorted multi dimensional array based on a particular key in php

我有一個數組,我想基於其中的特定索引創建一個多維數組。

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

使用此功能

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. 獲取所有日期,排序;

  2. 在一個循環中,找到一個與日期匹配的記錄,在''索引中添加所有對它的結果數組的跟蹤;

試試這個:

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

        )

)

嘗試這個:

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

看工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM