php convert multi dimension resultant array on basis of month array -
below resultant array , month array
$resultval = array ( [0] => array ( [0] => array ( [crate] => 13.51 [ucount] => 36 [udate] => 2016-06-30 ) ) ) $montharray =array("june 2016","july 2016");
i m using array_combine , loop through resultant array , using in_array function loop through each month resultant array below trying code,
$final_array = array_combine ( $montharray, array_fill ( 0, count ( $montharray ), 0 ) ); foreach ( $new_array $item ) { $month = $item ['udate']; if (in_array ( $month, $montharray )) { $final_array [$month] += $item ['crate']; } } $i = 0; $finalarray = array (); foreach ( $final_array $key => $value ) { $finalarray [$i] ['crate'] = "$value"; $finalarray [$i] ['udate'] = date ( "m js y", strtotime ( $key ) ); $i ++; }
i getting udate , create want ucount. below expected result is,
array ( [0] => array ( [crate] => 13.51 [ucount] => 36 [udate] => june 2016 ) [1] => array ( [crate] => 0 [ucount] => 0 [udate] => july 2016 ) )
do want this?
<?php $resultval = array(0 => array (0 => array ( 'crate' => '13.51', 'ucount' => '36', 'udate' => '2017-06-30', )) , array( 1 => array ( 'crate' => '70', 'ucount' => '25', 'udate' => '2017-06-30', ) ) ); $montharray =array("june 2016","july 2016"); $keys = array_keys($montharray); $length = count($keys); $c = array(); foreach($resultval $sub) { if(!is_array($sub)) { $sub = array(); } $c[] = array_combine(array_pad($keys, count($sub), null), array_pad(array_values($sub), $length, null)); // pad removes possibility of warning } $i = 0; $finalarray = array (); foreach ( $c $key => $value ) { $finalarray [$i] ['crate'] = $value[0]['crate']; $finalarray [$i] ['ucount'] = $value[0]['ucount']; $finalarray [$i] ['udate'] = date ( "m js y", strtotime ( $value[0]['udate'] ) ); $i ++; } print_r( $finalarray); ?>
Comments
Post a Comment