shalin shalin - 6 months ago 7
PHP Question

Get the data with key values

<?php
$year = array(2015,2016);
$no = array(1,2,3,4,5,6,7,8,9,10,11,12);

$dr = [];
foreach ($year as $yr)
{
$ar = [];
foreach ($no as $ss)
{
$aer['value'] = [12];

}

array_push($dr,$ar);

}
//print_r($dr);
?>


My php code looks like this.. The output of the following code looks as below:

array([0]=>array([2015]=>array([0]=>array([value]=12)...

But I need the output in the following format.. How to get the output in the following format.

array(
[2015]=>array
(
[0]=>array([value]=>12)
[1]=>array([value]=>12)
:
:
[11]=>array([value]=>12)

)
[2016]=>array
(
[0]=>array([value]=>12)
[1]=>array([value]=>12)
:
:
[11]=>array([value]=>12)

)
)

Answer

Make it easier: Working Example

Loop through the year and for each year you need to loop again the number of month. And you need store the value of the 12 as you want in to the forst index as the first array and inner array the 12 number and all the number also contain the array of index value and value 12.

$year = array(2015,2016);
$no = array(1,2,3,4,5,6,7,8,9,10,11,12);

$dr = [];
foreach ($year as $yr){
    foreach($no as $ss){
        $dr[$yr][] = array("value" => 12);
    }
}
print_r($dr);