0

I have the below array

Array
(
    [0] => Array
        (
            [reservation_time] => 08:30
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-14
        )

    [1] => Array
        (
            [reservation_time] => 09:00
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-14
        )

    [2] => Array
        (
            [reservation_time] => 09:30
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-14
        )

    [3] => Array
        (
            [reservation_time] => 08:30
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-15
        )

    [4] => Array
        (
            [reservation_time] => 09:00
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-15
        )

    [5] => Array
        (
            [reservation_time] => 09:30
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-15
        )

    [6] => Array
        (
            [reservation_time] => 08:30
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-16
        )

)

I like the array should be

Array(
 [2011-07-14] => Array
    (
            [0] => Array
                (
                    [reservation_time] => 08:30
                    [user_name] => 
                    [reason] => 
                    [comments] => recursive
                    [reservation_date] => 2011-07-14
                )

            [1] => Array
                (
                    [reservation_time] => 09:00
                    [user_name] => 
                    [reason] => 
                    [comments] => recursive
                    [reservation_date] => 2011-07-14
                )
    )
 [2011-07-15]  => Array
 ( 
    [0] => Array
        (
            [reservation_time] => 08:30
            [user_name] => 
            [reason] => 
            [comments] => recursive
            [reservation_date] => 2011-07-15
        )
)

How can i loop this array to get the desired output

Regards Nisanth

4 Answers 4

6
$new_array = array();
foreach($array as $item) {
  $new_array[$item['reservation_date']][] = $item;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was about to answer pretty the same, +1 to you
1

Sometimes it's nice to get a multidimensional array grouped by subkey - any subkey. This is basically like the accepted answer but overloading the actual array:

print_r($array('reservation_date')); # group by subkey 'reservation_date'

Code (Demo):

$array = array(
  0 => array(
    'reservation_time' => '08:30',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-14',
  ),
  1 => array(
    'reservation_time' => '09:00',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-14',
  ),
  2 => array(
    'reservation_time' => '09:30',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-14',
  ),
  3 => array(
    'reservation_time' => '08:30',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-15',
  ),
  4 => array(
    'reservation_time' => '09:00',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-15',
  ),
  5 => array(
    'reservation_time' => '09:30',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-15',
  ),
  6 => array(
    'reservation_time' => '08:30',
    'user_name' => '',
    'reason' => '',
    'comments' => 'recursive',
    'reservation_date' => '2011-07-16',
  ),
);

// overload $array with a function
$array = function($key) use ($array) {
    $r = array();
    foreach($array as $v) 
        $r[$v[$key]][] = $v;
    return $r;
};

// request subkey per parameter:

print_r($array('reservation_date'));

print_r($array('reservation_time'));

Comments

0

From my own PHP code, I have function for this. Just call make_assoc_from_list($your_array, 'reservation_date'):

// makes associative array from normal list
//      - $list - list
//      - $key - key of each item which will be the associative key
//

function make_assoc_from_list($list, $key)
{
        $assoc = array();
        if ($list === null)
                $list = array();
        foreach ($list as $item)
                $assoc[$item[$key]] = $item;
        return $assoc;
}

Comments

-1
$new = array();
foreach ($array as $entry) {
    if (!array_key_exists($entry['reservation_date'], $new)) $new[$entry['reservation_date']] = array();
    $new[$entry['reservation_date']][] = $entry;
}

1 Comment

yes, pressed TAB and SPACE...and answer was commited :( so please remove downvote

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.