eval.in

Paste #657257

PHP — PHP 5.6.23, pasted 1 year ago (json)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

$data = array(
  array("date" => "2016-01", "ids" => array(1,2,3,4,5,6,7,8)),
  array("date" => "2016-02", "ids" => array(1,2,9,10,11,12)),
  array("date" => "2016-03", "ids" => array(3,16,17,18,19,20,21)),
  array("date" => "2016-04", "ids" => array(1,2,3,19,20,22,23))
);

$newArray = array();
$count = count($data);
for($i = 0; $i < $count; ++$i){
	$newArray[$i] = array('date' => $data[$i]['date'], 'counter' => array());
	for($j = 0; $j < $i; ++$j){
		$counter = 0;
		foreach($data[$i]['ids'] as $key => $id){
			if(in_array($id, $data[$j]['ids'])){
				++$counter;
				unset($data[$i]['ids'][$key]);
			}
		}
		$newArray[$i]['counter'][] = $counter;
	}
	$newArray[$i]['counter'][] = count($data[$i]['ids']);
}

// display $newArray array
echo '<pre>'; print_r($newArray);

Program Output

<pre>Array
(
    [0] => Array
        (
            [date] => 2016-01
            [counter] => Array
                (
                    [0] => 8
                )

        )

    [1] => Array
        (
            [date] => 2016-02
            [counter] => Array
                (
                    [0] => 2
                    [1] => 4
                )

        )

    [2] => Array
        (
            [date] => 2016-03
            [counter] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 6
                )

        )

    [3] => Array
        (
            [date] => 2016-04
            [counter] => Array
                (
                    [0] => 3
                    [1] => 0
                    [2] => 2
                    [3] => 2
                )

        )

)

OK (0.016 sec real, 0.019 sec wall, 14 MB, 675 syscalls)

Fork