[英]Get the desired output in 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.
我不擅长 foreach 循环,但我怀疑它是否会解决问题。
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.在 $uniqueDates 中,我采用了所有唯一日期,$data 是包含上述格式数据的数组。
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.