[Chartjs]-Php add dummy data to array

1πŸ‘

βœ…

Here in this solution if there is no date for any one the id

then there will a date with 0 value will be added

$a = array(
        array('id' => '1','date' => '09-04-2018','length' => '10'),
        array('id' => '2','date' => '09-04-2018','length' => '20'),
        array('id' => '1','date' => '10-04-2018','length' => '11'),
        array('id' => '2','date' => '10-04-2018','length' => '21'),
        array('id' => '1','date' => '11-04-2018','length' => '12'),
        array('id' => '1','date' => '12-04-2018','length' => '13'),
        array('id' => '2','date' => '12-04-2018','length' => '23'),
        array('id' => '1','date' => '13-04-2018','length' => '14')
    );

$b = array_unique(array_column($a,'date'));
$a1 = $a2 = [];

foreach ($b as $key => $date) {
    $a1[1][$date] = 0;
    $a1[2][$date] = 0;
    foreach ($a as $key2 => $value2) {
        if($value2['id'] == 1){
            if($date == $value2['date']){
                $a1[1][$date] = $value2['length'];
            }
        }elseif($value2['id'] == 2){
            if($date == $value2['date']){
                $a1[2][$date] = $value2['length'];
            }
        }
    }
}

For more than 2 datasets

$b = array_unique(array_column($a,'date'));
$sets = array_unique(array_column($a,'id'));
$a1 = $a2 = [];

foreach ($sets as $s) {
    foreach ($b as $key => $date) {
        $a1[$s][$date] = 0;
        foreach ($a as $key2 => $value2) {
            if($value2['id'] == $s){
                if($date == $value2['date']){
                    $a1[$s][$date] = $value2['length'];
                }
            }
        }
    }   
}

echo "<pre>";
print_r($a1);
echo "</pre>";

OUTPUT

Array
(
    [1] => Array
        (
            [09-04-2018] => 10
            [10-04-2018] => 11
            [11-04-2018] => 12
            [12-04-2018] => 13
            [13-04-2018] => 14
        )

    [2] => Array
        (
            [09-04-2018] => 20
            [10-04-2018] => 21
            [11-04-2018] => 0
            [12-04-2018] => 23
            [13-04-2018] => 0
        )

)

1πŸ‘

INPUT

$array = array(
    array('id' => 1,'date' => '09-04-2018','length' => 10),
    array('id' => 2,'date' => '09-04-2018','length' => 20),
    array('id' => 1,'date' => '10-04-2018','length' => 11),
    array('id' => 2,'date' => '10-04-2018','length' => 21),
    array('id' => 1,'date' => '11-04-2018','length' => 12),
    array('id' => 1,'date' => '12-04-2018','length' => 13),
    array('id' => 2,'date' => '12-04-2018','length' => 23),
    array('id' => 1,'date' => '13-04-2018','length' => 14)
);

SOLUTION

    $id = array();
foreach($array as $r){
    $data[$r['id']][$r['date']] = $r['length'];
    if(!in_array($r['id'],$id)) $id[] = $r['id'];
}
for($i=0;$i<count($id);$i++){
    $cid = $id[$i];
    $nid = $id;
    unset($nid[$i]);
    foreach($data[$cid] as $k => $r){
        foreach($nid as $n){
            if(!isset($data[$n][$k]))$data[$n][$k] = 0;
        }
    }
}
echo "<pre>";print_r($data);

OUTPUT

Array
(
    [1] => Array
        (
            [09-04-2018] => 10
            [10-04-2018] => 11
            [11-04-2018] => 12
            [12-04-2018] => 13
            [13-04-2018] => 14
        )

    [2] => Array
        (
            [09-04-2018] => 20
            [10-04-2018] => 21
            [11-04-2018] => 0
            [12-04-2018] => 23
            [13-04-2018] => 0
        )

)

0πŸ‘

Just add one condition and you will get as u required

$data_set = array();
    foreach ($array as $item)
    { 
        $data_set[$item['id']][$item['date']] = (($item['length'] == 0)?0:$item['length']);
    }
echo '<pre>'; print_r($data_set); echo '</pre>';exit();

-1πŸ‘

<?php
$dataset=array();
for ($i=0;$i<=10;$i++){
    for ($k=0;$k<=100;$k++){
$int= mt_rand(time()-1000000,time());
$dataset[$i][date("Y-m-d",$int)] = mt_rand(0,30) ;
}
}

print_r($dataset);

You will get something like

Array
(
    [0] => Array
        (
            [2018-04-16] => 19
            [2018-04-11] => 24
            [2018-04-09] => 21
            [2018-04-10] => 7
            [2018-04-06] => 19
            [2018-04-12] => 11
            [2018-04-14] => 3
            [2018-04-07] => 19
            [2018-04-15] => 20
            [2018-04-13] => 2
            [2018-04-17] => 29
            [2018-04-08] => 0
        )

    [1] => Array
        (
            [2018-04-09] => 12
            [2018-04-13] => 13
            [2018-04-11] => 23
            [2018-04-06] => 28
            [2018-04-07] => 24
            [2018-04-15] => 26
            [2018-04-08] => 19
            [2018-04-12] => 24
            [2018-04-17] => 24
            [2018-04-16] => 26
            [2018-04-14] => 17
            [2018-04-05] => 24
            [2018-04-10] => 17
        )

    [2] => Array
        (
            [2018-04-14] => 29
            [2018-04-08] => 25
            [2018-04-15] => 17
            [2018-04-11] => 30
            [2018-04-06] => 0
            [2018-04-10] => 9
            [2018-04-12] => 7
            [2018-04-13] => 4
            [2018-04-07] => 23
            [2018-04-16] => 22
            [2018-04-09] => 12
            [2018-04-17] => 14
        )

    [3] => Array
        (
            [2018-04-10] => 22
            [2018-04-13] => 14
            [2018-04-11] => 6
            [2018-04-16] => 5
            [2018-04-15] => 27
            [2018-04-14] => 21
            [2018-04-09] => 2
            [2018-04-06] => 5
            [2018-04-08] => 20
            [2018-04-12] => 6
            [2018-04-17] => 6
            [2018-04-05] => 5
            [2018-04-07] => 23
        )

    [4] => Array
        (
            [2018-04-06] => 20
            [2018-04-05] => 20
            [2018-04-09] => 5
            [2018-04-07] => 25
            [2018-04-15] => 12
            [2018-04-13] => 22
            [2018-04-10] => 3
            [2018-04-16] => 11
            [2018-04-11] => 16
            [2018-04-14] => 5
            [2018-04-12] => 7
            [2018-04-17] => 25
            [2018-04-08] => 12
        )

    [5] => Array
        (
            [2018-04-07] => 24
            [2018-04-15] => 3
            [2018-04-12] => 13
            [2018-04-06] => 18
            [2018-04-09] => 4
            [2018-04-08] => 2
            [2018-04-10] => 2
            [2018-04-11] => 14
            [2018-04-16] => 17
            [2018-04-13] => 0
            [2018-04-14] => 25
            [2018-04-17] => 30
            [2018-04-05] => 20
        )

    [6] => Array
        (
            [2018-04-06] => 5
            [2018-04-16] => 6
            [2018-04-13] => 18
            [2018-04-08] => 5
            [2018-04-11] => 11
            [2018-04-15] => 18
            [2018-04-12] => 8
            [2018-04-14] => 23
            [2018-04-05] => 0
            [2018-04-17] => 22
            [2018-04-09] => 27
            [2018-04-10] => 25
            [2018-04-07] => 19
        )

    [7] => Array
        (
            [2018-04-07] => 2
            [2018-04-15] => 6
            [2018-04-16] => 24
            [2018-04-08] => 24
            [2018-04-09] => 0
            [2018-04-10] => 21
            [2018-04-11] => 26
            [2018-04-13] => 4
            [2018-04-12] => 25
            [2018-04-06] => 16
            [2018-04-14] => 17
            [2018-04-05] => 13
            [2018-04-17] => 16
        )

    [8] => Array
        (
            [2018-04-10] => 6
            [2018-04-16] => 2
            [2018-04-12] => 7
            [2018-04-15] => 15
            [2018-04-07] => 25
            [2018-04-09] => 17
            [2018-04-08] => 25
            [2018-04-06] => 4
            [2018-04-11] => 29
            [2018-04-14] => 6
            [2018-04-13] => 15
            [2018-04-17] => 28
            [2018-04-05] => 20
        )

    [9] => Array
        (
            [2018-04-09] => 2
            [2018-04-11] => 14
            [2018-04-14] => 20
            [2018-04-07] => 3
            [2018-04-13] => 23
            [2018-04-17] => 3
            [2018-04-16] => 16
            [2018-04-15] => 2
            [2018-04-06] => 27
            [2018-04-12] => 5
            [2018-04-10] => 26
            [2018-04-08] => 3
        )

    [10] => Array
        (
            [2018-04-07] => 20
            [2018-04-11] => 28
            [2018-04-14] => 4
            [2018-04-05] => 13
            [2018-04-16] => 0
            [2018-04-09] => 27
            [2018-04-08] => 22
            [2018-04-06] => 30
            [2018-04-12] => 14
            [2018-04-13] => 5
            [2018-04-15] => 18
            [2018-04-17] => 4
            [2018-04-10] => 21
        )

)

Leave a comment