get value as column format from php array -
i´m working google line chart. want prepare info explained here.i have php array given below. need each array value assigned in 1 php variable individual column (like below of array) dynamically. how can accomplish this?
array ( [1] => array ( [1] => c1 [2] => c1 [3] => c1 [4] => c1 [5] => c1 [6] => c1 [7] => c1 [8] => c1 [9] => c1 [10] => c1 [11] => c1 [12] => c1 ) [10] => array ( [1] => c2 [2] => c2 [3] => c2 [4] => c2 [5] => c2 [6] => c2 [7] => c2 [8] => c2 [9] => c2 [10] => c2 [11] => c2 [12] => c2 ) )
expected result is
$output=” ['1', c1, c2], ['2', c1, c2], . . . ['12', c1, c2], “;
two nested loops should trick in case:
$input = /* input array */; $grouped = array(); foreach ($input $elements) { foreach ($elements $key => $element) { $grouped[$key][] = $element; } }
this produces array looks following:
array( 1 => array(c1, c2, ...), 2 => array(c1, c2, ...), ... );
this can converted desired output:
$output = ""; foreach ($grouped $name => $values) { $output .= "['$name'"; foreach ($values $value) $output .= ", $value"; $output .= "],\n"; }
which produce:
$output = "['1', c1, c2],\n['2', c1, c2]\n,...";
php arrays linechart
No comments:
Post a Comment