php - How to maintain array structure and keys during calcultations -
i have database rows of data each column country. in example there 4 country (columns) , 3 rows although number of each change , need dynamic inputs. want normalize each row min value 0 , max 100 , @ same time keep 4 x 3 array structure of original database can extract rows or columns on request.
the final output passed javascript presented in graph form. code have come normalizes data correctly output 1 long array 12 x 1 , column names have been dropped.
i wondering if know how keep same structure $scorenorm
output of $dataa
, both presented below?
<?php $conn=mysql_connect("relevant inputs"); if(! $conn ) { die('could not connect: ' . mysql_error()); } mysql_select_db("db-name"); $dataarray = "select * data2012" or die(mysql_error()); $data2012=mysql_query($dataarray, $conn); if(! $data2012 ) { die('could not data: ' . mysql_error()); } $scorenorm = array(); $dataa = array(); while($row = mysql_fetch_assoc($data2012)) { $dataa[] = $row; $max_val = max($row); $min_val = min($row); foreach($row $key => &$dataaitem) { $scorenorm[] = array((($dataaitem - $min_val)/($max_val - $min_val))*100); } } echo json_encode($dataa) . "<br />"; echo json_encode($scorenorm) . "<br />"; echo $dataa[1]['france']; mysql_close($conn); ?>
the output echo json_encode($dataa) is
[{"china":"11.000","australia":"8.300","france":"12.600","uk":"6.220"},{"china":"2.000","australia":"1.000","france":"4.000","uk":"5.000"},{"china":"39548.000","australia":"25487.000","france":"245.000","uk":"2547852.000"}]
where output echo json_encode($scorenorm) is
[[74.921630094],[32.6018808777],[100],[0],[25],[0],[75],[100],[1.54274187502],[0.990812162158],[0],[100]]
to reiterate, $scorenorm
maintain $dataa format
. ideas appreciated.
i have cracked second foreach out own loop because dont know column names using, , have fetched point. operating under assumption country name key, , number value. if need that, let me know column names in structure, otherwise check out:
$dataa = array(); $scorenorm = array(); while($row = mysql_fetch_assoc($data2012)) { $dataa[] = $row; } foreach($dataa $dataakey => $dataavar){ $max_val = max($dataavar); $min_val = min($dataavar); $scorenormtemp=array(); foreach($row $country => $value) { $scorenormtemp[$country] = array((($value - $min_val)/($max_val - $min_val))*100); } $scorenorm[] = $scorenormtemp; }
you populating array 1 dimensionally, when need make new array gets added array of arrays @ end.
Comments
Post a Comment