1

I am trying to populate an array so that I can plot a chart using chart.js

I have an array that looks like:

[0] => Array
    (
        [id] => 1
        [date] => 09-04-2018
        [length] => 10
    )

[1] => Array
    (
        [id] => 2
        [date] => 09-04-2018
        [length] => 20
    )

[2] => Array
    (
        [id] => 1
        [date] => 10-04-2018
        [length] => 11
    )

[3] => Array
    (
        [id] => 2
        [date] => 10-04-2018
        [length] => 21
    )

[4] => Array
    (
        [id] => 1
        [date] => 11-04-2018
        [length] => 12
    )

[5] => Array
    (
        [id] => 1
        [date] => 12-04-2018
        [length] => 13
    )

[6] => Array
    (
        [id] => 2
        [date] => 12-04-2018
        [length] => 23
    )

[7] => Array
    (
        [id] => 1
        [date] => 13-04-2018
        [length] => 14
    )

I need to split this array into 2 datasets (id 1 and id 2) however I cannot have gaps in these datasets as chartJS does not like it.

I need the array to look like:

[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
)

using the following code:-

foreach ($array as $item)
{
    $id = $item['id'];
    $date = $item['date'];
    $length = $item['length'];

    $output[$id][$date] = $length;
}

I can produce an array in the format I like but as the initial array is missing 2 dates for dataset 2 I do not get these added in and the result is:-

[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
    [12-04-2018] => 23
)

is there a way to populate the array the way I want so if 1 of the datasets uses that date, both(all it maybe 3 or 4 datasets) have to use that date but by simply adding in a 0?

2
  • You need to fill up the interpolation. After this iteration, you should check what is first key date, and last key date for each array. Then do the date iterations between these dates and check if isset that key. If not set, you should add this key. Commented Apr 17, 2018 at 10:18
  • Check this solution stackoverflow.com/questions/35947738/… Commented Apr 17, 2018 at 10:21

4 Answers 4

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
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic!!! But what if I had more than 2 datasets? I may have 1, 2, 3 or 4 datasets depending on certain conditions?
@Chris Now you can add any number of datasets. :)
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
        )

)

2 Comments

Nice, however this only works if dataset 2 has missing dates, dataset 1 could have missing dates that dataset 2 has! also ther could be upto 4 datasets
Thank you, this works just aswell as the accepted answer however I prefer the method from @Jitendra Softgrid so have chosen his answer as the accepted one
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();

2 Comments

this is creating the exact same array that I am? with the gaps. the only difference is you are adding it to the initial array rahter than creating a new one? I need this to populate dataset 2 with all the same dates from dataset 1 or vice versa.
You can use ternary operator while assining data
-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
        )

)

2 Comments

this is creating an array the same way that I am? however you are populating it with random dates and data rather than splitting my initial array? Have I missed something because I cant see how this helps me?
Do you understand what "populate" means?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.