I want to merge objects wich has same id.
I am getting javascript objects as a string from mysql with php:
$sql="SELECT plaka,il,SUM(hasta_say) AS Hasta_Say FROM ADSM GROUP BY plaka,il ORDER BY Hasta_Say ";
///COLUMN CHARTS
$result=$baglanti->query($sql);
$ilEventChart="";
while ($query=$result->fetch(PDO::FETCH_ASSOC)) {
$color=random_color();
$ilEventChart.='
columnChartData.TR'.$query['plaka'].' = [
{
"il": "'.$query['il'].'",
"hasta_sayisi":'.$query['Hasta_Say'].',
"color": "'.$color.'"
}];
';
}
So I am printing to $ilEventChart
php variable to javascript area.
After this javascript Objects created Like this.But recursive 'plaka' causing recursive objects with different properties.
Here Objects:
columnChartData.TR78 = [
{
"il": "KARABÜK",
"hasta_sayisi":66148,
"color": "b2147e"
}];
columnChartData.TR78 = [
{
"il": "ÇANAKKALE",
"hasta_sayisi":66246,
"color": "9a720a"
}];
I want to merge this objects as a one object.Desired output like this:
columnChartData.TR78 = [
{
"il": "KARABÜK",
"hasta_sayisi":66148,
"color": "b2147e"
},
{
"il": "ÇANAKKALE",
"hasta_sayisi":66246,
"color": "9a720a"
}];
How can we do this with javascript?
Thanks
After long working I found the solution.Solution possible with using PHP Arrays.
Here is Solution:
birim
MultiDimensional Array for using javascript id(plaka). After We get datas from MySQL with while loop and push the every datas to birim
array
So we created a multidimensional array with id number wich has own key.For example if we check the array wtih print_r
:
echo '
'; \nprint_r($birim[6]);
We saw about id of 6
values in array:
Array
(
[0] => {
"birim": "ANKARA GÖLBAÅI AÄIZ VE DÄ°Å SAÄLIÄI MERKEZÄ°",
"hasta_sayisi":70518,
"color": "e817c0"
},
[1] => {
"birim": "ANKARA SÄ°NCAN AÄIZ VE DÄ°Å SAÄLIÄI MERKEZÄ°",
"hasta_sayisi":77868,
"color": "ac31aa"
},
[2] => {
"birim": "ANKARA BALGAT AÄIZ VE DÄ°Å SAÄLIÄI MERKEZÄ°",
"hasta_sayisi":97564,
"color": "609724"
},
[3] => {
"birim": "ANKARA KEÇİÖREN OSMANLI AÄIZ VE DÄ°Å SAÄLIÄI MERKEZÄ°",
"hasta_sayisi":97773,
"color": "62f1a0"
},
[4] => {
"birim": "ANKARA MAMAK AÄIZ VE DÄ°Å SAÄLIÄI MERKEZÄ°",
"hasta_sayisi":126806,
"color": "2c7498"
},
[5] => {
"birim": "ANKARA TOPRAKLIK AÄIZ VE DÄ°Å SAÄLIÄI MERKEZÄ°",
"hasta_sayisi":126854,
"color": "23a203"
},
[6] => {
"birim": "ANKARA TEPEBAÅI AÄIZ VE DÄ°Å SAÄLIÄI HASTANESÄ°",
"hasta_sayisi":200623,
"color": "735aa6"
},
[7] => {
"birim": "ANKARA 75.YIL AÄIZ VE DÄ°Å SAÄLIÄI HASTANESÄ°",
"hasta_sayisi":260057,
"color": "4d8cd1"
},
)
foreach loop
to temp variable which we will use in javascript code.Here is Full Codes:
$sql="SELECT plaka,birim,SUM(hasta_say) AS Hasta_Say FROM ADSM GROUP BY birim ORDER BY Hasta_Say "; $result=$baglanti->query($sql); $birimEventChart=""; $birim=array(); while ($query=$result->fetch(PDO::FETCH_ASSOC)) { $color=random_color(); $birim[$query['plaka']][]='{ "birim": "'.$query['birim'].'", "hasta_sayisi":'.$query['Hasta_Say'].', "color": "'.$color.'" },'; } $sql="SELECT DISTINCT plaka FROM ADSM"; $result=$baglanti->query($sql); $temp=""; while ($query=$result->fetch(PDO::FETCH_ASSOC)) { foreach ($birim[$query['plaka']] as $p) { $temp.=$p; } $birimEventChart.='hastaneChartData.TR'.$query['plaka'].' = ['.$temp.'];'; $temp=""; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.