简体   繁体   中英

how can i get two dimensional array in this format?

I got an array in this format.

   Date            Employee               Notes
   2013-03-08        ABC                 Notes of ABC on 08-03-2013
   2013-03-08        PQR                 Notes of PQR on 08-03-2013
   2013-03-08        XYZ                 Notes of XYZ on 08-03-2013
   2013-03-09        ABC                 Notes of ABC on 09-03-2013
   2013-03-09        PQR                 Notes of PQR on 09-03-2013
   2013-03-09        XYZ                 Notes of XYZ on 09-03-2013

And i want the result array in this format

 Date            Employee               Notes
 2013-03-08        ABC                 Notes of ABC on 08-03-2013
                   PQR                 Notes of PQR on 08-03-2013
                   XYZ                 Notes of XYZ on 08-03-2013
 2013-03-09        ABC                 Notes of ABC on 09-03-2013
                   PQR                 Notes of PQR on 09-03-2013
                   XYZ                 Notes of XYZ on 09-03-2013

So how can i write my php code to get result like this ?? This is a two dimensional array and i have used foreach loop to display like this.

Can anybody please help me ?

You just have to iterate on the initial array, and store in a var the current date. while the next date is not changing, you store the sub data in the same sub entry.

Assuming you have

$initial = array(
    array('2013-03-08','ABC','Notes of ABC on 08-03-2013'),
    array('2013-03-08','PQR','Notes of PQR on 08-03-2013'),
    array('2013-03-08','XYZ','Notes of XYZ on 08-03-2013'),
    array('2013-03-09','ABC','Notes of ABC on 09-03-2013'),
    array('2013-03-09','PQR','Notes of PQR on 09-03-2013'),
    array('2013-03-09','XYZ','Notes of XYZ on 09-03-2013')
)

You can make your new array like

$final = array();
$currentDate = false;
foreach($initial as $index => $subArray)
{
    if ($currentDate === false || $currentDate != $subArray[0])
    {
        $currentDate = $subArray[0];
        $final[$currentDate] = array();
    }

    $final[$currentDate][] = array($subArray[1], $subArray[2]);
}

This will work:

$entries = array(
    array("Date" => "2013-03-08", "Employee" => "ABC", "Notes" => "Notes of ABC on 08-03-2013"),
    array("Date" => "2013-03-08", "Employee" => "PQR", "Notes" => "Notes of PQR on 08-03-2013"),
    array("Date" => "2013-03-08", "Employee" => "XYZ", "Notes" => "Notes of XYZ on 08-03-2013"),
    array("Date" => "2013-03-09", "Employee" => "ABC", "Notes" => "Notes of ABC on 09-03-2013"),
    array("Date" => "2013-03-09", "Employee" => "PQR", "Notes" => "Notes of PQR on 09-03-2013"),
    array("Date" => "2013-03-09", "Employee" => "XYZ", "Notes" => "Notes of XYZ on 09-03-2013")
);

$y = array();
foreach ($entries as $entry) {
    $date = $entry["Date"];
    if (!isset($y[$date])) $y[$date] = array();
    unset($entry["Date"]);
    $y[$date][] = $entry;
}

print_r($y);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM