duanchu7271 2017-01-14 11:28
浏览 25
已采纳

在PHP中获得所需的输出

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.

  • 写回答

1条回答 默认 最新

  • doue8385 2017-01-14 11:48
    关注

    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
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题