-1

I have a data in the below format

$data = 
 Array
(
[0] => Array
    (
        [cdt] => 2016-01-01
        [name] => Harish
        [hour] => 8.5
    )

[1] => Array
    (
        [cdt] => 2016-01-01
        [name] => Preeti
        [hour] => 8
    )

[2] => Array
    (
        [cdt] => 2016-01-01
        [name] => Sunit
        [hour] => 9
    )

[3] => Array
    (
        [cdt] => 2016-01-01
        [name] => Prabhakar
        [hour] => 8
    )

[4] => Array
    (
        [cdt] => 2016-01-02
        [name] => Prabhakar
        [hour] => 2
    )

[5] => Array
    (
        [cdt] => 2016-01-04
        [name] => Preeti
        [hour] => 8
    )

[6] => Array
    (
        [cdt] => 2016-01-04
        [name] => Sunit
        [hour] => 9
    )

[7] => Array
    (
        [cdt] => 2016-01-04
        [name] => Harish
        [hour] => 9.5
    )

[8] => Array
    (
        [cdt] => 2016-01-04
        [name] => Prabhakar
        [hour] => 10
    )

[9] => Array
    (
        [cdt] => 2016-01-05
        [name] => Preeti
        [hour] => 10
    )

[10] => Array
    (
        [cdt] => 2016-01-05
        [name] => Sunit
        [hour] => 8
    )

[11] => Array
    (
        [cdt] => 2016-01-05
        [name] => Harish
        [hour] => 9
    )

[12] => Array
    (
        [cdt] => 2016-01-05
        [name] => Prabhakar
        [hour] => 9
    )
)

I want it to be in this format

Array
(
  [0] => 2016-01-01, Harish: 8.5, Preeti: 8, Sunit: 9, Prabhakar: 8
  [1] => 2016-01-02, Prabhakar: 2
  [2] => 2016-01-04, Preeti: 8, Sunit: 9, Harish: 9.5, Prabhakar: 10
  [3] => 2016-01-05, Preeti: 10, Sunit: 8, Harish: 9, Prabhakar: 9
)

I have tried some loops but those were not helpful. I am not good in foreach loop, but I have a doubt that it will solve the problem.

Below is the code that I have tried

$k=0;
for($i = 0; $i < sizeOf($uniqueDates); $i++)
{
    $chartData[$i] = 'y: '.$data[$i]['cdt'].', ';
    for($j = $k; $j < Sizeof($data); $j++)
    {
        if($uniqueDates[$i] == $data[$j]['cdt'])
        {
            $chartData[$i] .= $data[$j]['name'].': '.$data[$j]['hour'].', ';
        }

    }
    $k++;

}

In $uniqueDates I have taken all the unique dates and $data is the array that contains the data in the above format.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

This looks pretty straight forward. All you have to do is organize your entries and then format them according to what output you want.

Take a look at this simple example:

<?php

$data = [
    [
        'cdt' => '2016-01-01',
        'name' => 'Harish',
        'hour' => '10'
    ],
    [
        'cdt' => '2016-01-01',
        'name' => 'Preeti',
        'hour' => '8'
    ],
    [
        'cdt' => '2016-01-01',
        'name' => 'Sunit',
        'hour' => '9'
    ],
    [
        'cdt' => '2016-01-01',
        'name' => 'Prabhakar',
        'hour' => '8'
    ],
    [
        'cdt' => '2016-01-02',
        'name' => 'Prabhakar',
        'hour' => '2'
    ],
    [
        'cdt' => '2016-01-04',
        'name' => 'Preeti',
        'hour' => '8'
    ],
    [
        'cdt' => '2016-01-04',
        'name' => 'Sunit',
        'hour' => '9'
    ],
    [
        'cdt' => '2016-01-04',
        'name' => 'Harish',
        'hour' => '9.5'
    ],
    [
        'cdt' => '2016-01-04',
        'name' => 'Prabhakar',
        'hour' => '10'
    ],
    [
        'cdt' => '2016-01-05',
        'name' => 'Preeti',
        'hour' => '10'
    ],
    [
        'cdt' => '2016-01-05',
        'name' => 'Sunit',
        'hour' => '8'
    ],
    [
        'cdt' => '2016-01-05',
        'name' => 'Harish',
        'hour' => '9'
    ],
    [
        'cdt' => '2016-01-05',
        'name' => 'Prabhakar',
        'hour' => '9'
    ]
];

$catalog = [];
foreach ($data as $entry) {
    $catalog[$entry['cdt']][] = sprintf('%s: %s', $entry['name'], $entry['hour']);
}

foreach ($catalog as $cdt => $entries) {
    $output[] = sprintf('%s, %s', $cdt, implode(', ', $entries));
}

print_r($output);

The output of above code obviously is:

Array
(
    [0] => 2016-01-01, Harish: 10, Preeti: 8, Sunit: 9, Prabhakar: 8
    [1] => 2016-01-02, Prabhakar: 2
    [2] => 2016-01-04, Preeti: 8, Sunit: 9, Harish: 9.5, Prabhakar: 10
    [3] => 2016-01-05, Preeti: 10, Sunit: 8, Harish: 9, Prabhakar: 9
)
arkascha
  • 41,620
  • 7
  • 58
  • 90