0

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

4 Answers 4

2

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");
Sign up to request clarification or add additional context in comments.

1 Comment

on my test, I have to patch it like this: foreach ($array as $a) { if( !isset( $return[$a[$key]] ) ) $return[$a[$key]] = array(); $return[$a[$key]][] = $a; }
0

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;

Comments

0

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
                )

        )

)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.