Here is my multidimensional array:
Array (
[0] => Array ( [0] => 2015-06-01 [1] => active [2] => 345 )
[1] => Array ( [0] => 2015-06-01 [1] => not_active [2] => 337 )
[2] => Array ( [0] => 2015-06-01 [1] => active [2] => 328 )
[3] => Array ( [0] => 2015-06-02 [1] => not_active [2] => 312 )
[4] => Array ( [0] => 2015-06-02 [1] => active [2] => 338 )
[5] => Array ( [0] => 2015-06-02 [1] => active [2] => 346 )
[6] => Array ( [0] => 2015-06-03 [1] => not_active [2] => 317 )
[7] => Array ( [0] => 2015-06-03 [1] => active [2] => 339 )
[8] => Array ( [0] => 2015-06-04 [1] => active [2] => 340 )
[9] => Array ( [0] => 2015-06-04 [1] => not_active [2] => 318 )
[10] => Array ( [0] => 2015-06-05 [1] => active [2] => 322 )
[11] => Array ( [0] => 2015-06-05 [1] => not_active [2] => 341 )
[12] => Array ( [0] => 2015-06-06 [1] => active [2] => 342 )
[13] => Array ( [0] => 2015-06-06 [1] => active [2] => 325 )
[14] => Array ( [0] => 2015-06-07 [1] => active [2] => 343 )
[15] => Array ( [0] => 2015-06-07 [1] => active [2] => 326 )
)
There is 7 days, but 16 records. I'm trying to create a loop to combine records with same dates to have 7 records array. To look like this:
Array (
[0] => Array ( [0] => 2015-06-01 [1] => active [2] => 345 [3] => 2015-06-01 [4] => not_active [5] => 337[6] => 2015-06-01 [7] => active [8] => 328)
[1] => Array ( [0] => 2015-06-02 [1] => not_active [2] => 312 [3] => 2015-06-02 [4] => active [5] => 338 [6] => 2015-06-02 [7] => active [8] => 346)
[2] => Array ( [0] => 2015-06-03 [1] => not_active [2] => 317 [3] => 2015-06-03 [4] => active [5] => 339 )
[3] => Array ( [0] => 2015-06-04 [1] => active [2] => 340 [3] => 2015-06-04 [4] => not_active [5] => 318)
[4] => Array ( [0] => 2015-06-05 [1] => active [2] => 322 [3] => 2015-06-05 [4] => not_active [5] => 341 )
[5] => Array ( [0] => 2015-06-06 [1] => active [2] => 342 [3] => 2015-06-06 [4] => active [5] => 325 )
[6] => Array ( [0] => 2015-06-07 [1] => active [2] => 343 [3] => 2015-06-07 [4] => active [5] => 326)
)
The following uses the "dates" as the keys in the final output array:
<?php
// Initializing
$arr = [
['2015-06-01', 'active', 345],
['2015-06-01', 'not_active', 337],
['2015-06-01', 'active', 328],
['2015-06-02', 'not_active', 312],
['2015-06-02', 'active', 338],
['2015-06-02', 'active', 346],
['2015-06-03', 'not_active', 317],
['2015-06-03', 'active', 339],
['2015-06-04', 'active', 340],
['2015-06-04', 'not_active', 318],
['2015-06-05', 'active', 322],
['2015-06-05', 'not_active', 341],
['2015-06-06', 'active', 342],
['2015-06-06', 'active', 325],
['2015-06-07', 'active', 343],
['2015-06-07', 'active', 326],
];
// The heart of the code:
$out = array();
foreach($arr as $element) {
foreach($element as $val)
$out[$element[0]][] = $val;
}
// Printing
echo "<pre>";
print_r($out);
echo "</pre>";
?>
However, if you want the keys of the output array to be indexed sequentially, then you may also use the following code segment:
$out2 = array();
$i = 0;
foreach($out as $element)
$out2[$i++] = $element;
// Printing
echo "<hr>";
echo "<pre>";
print_r($out2);
echo "</pre>";
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加