Abu Ayyub Abu Ayyub - 1 year ago 86
MySQL Question

Mapping Mysql Result to Custom Json Format

I am trying to mapping the mysql result into json encode, but the result is not as i want.

Results From the Mysql Query

Mysql Result

print_r($data)


Array
(
[0] => Array
(
[Bulan] => Januari
[Total] => 1
[Langsung] => 0
[Delivery] => 1
[Dropship] => 0
)

[1] => Array
(
[Bulan] => Februari
[Total] => 24
[Langsung] => 19
[Delivery] => 4
[Dropship] => 1
)

[2] => Array
(
[Bulan] => Maret
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[3] => Array
(
[Bulan] => April
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[4] => Array
(
[Bulan] => Mei
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[5] => Array
(
[Bulan] => Juni
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[6] => Array
(
[Bulan] => Juli
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[7] => Array
(
[Bulan] => Agustus
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[8] => Array
(
[Bulan] => September
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[9] => Array
(
[Bulan] => Oktober
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[10] => Array
(
[Bulan] => November
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

[11] => Array
(
[Bulan] => Desember
[Total] => 0
[Langsung] => 0
[Delivery] => 0
[Dropship] => 0
)

)


If I use
echo json_encode($data);

The Result like This:

[
{
"Bulan": "Januari",
"Total": "1",
"Langsung": "0",
"Delivery": "1",
"Dropship": "0"
},
{
"Bulan": "Februari",
"Total": "24",
"Langsung": "19",
"Delivery": "4",
"Dropship": "1"
},
{
"Bulan": "Maret",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "April",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "Mei",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "Juni",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "Juli",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "Agustus",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "September",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "Oktober",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "November",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
},
{
"Bulan": "Desember",
"Total": "0",
"Langsung": "0",
"Delivery": "0",
"Dropship": "0"
}
]


My Expected Like This:

{
"Bulan" : ["Januari", "Februari", "Maret", "April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember"],
"Total" : [1,24,0,0,0,0,0,0,0,0,0,0,0],
"Langsung" : [0,19,0,0,0,0,0,0,0,0,0,0,0],
"Delivery" : [1,4,0,0,0,0,0,0,0,0,0,0,0],
"Dropship" : [0,1,0,0,0,0,0,0,0,0,0,0,0]
}


Is possible to do that?

Thanks in advance

Answer Source

there are many ways.. without testing anything i think it should look something like this

$output=array();
foreach($data as $entry)
foreach($entry as $k=>$v)
{
if(!is_array($output[$k]))
$output[$k]=array();
$output[$k][]=$v;
}

print_r($output);
echo json_encode($output);

the output that i get is the following

{"Bulan":["Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember"],
"Total":["1","24","0","0","0","0","0","0","0","0","0","0"],
"Langsung":["0","19","0","0","0","0","0","0","0","0","0","0"],
"Delivery":["1","4","0","0","0","0","0","0","0","0","0","0"],
"Dropship":["0","1","0","0","0","0","0","0","0","0","0","0"]}.

full code used:

<?php

$data=json_decode('[
  {
    "Bulan": "Januari",
    "Total": "1",
    "Langsung": "0",
    "Delivery": "1",
    "Dropship": "0"
  },
  {
    "Bulan": "Februari",
    "Total": "24",
    "Langsung": "19",
    "Delivery": "4",
    "Dropship": "1"
  },
  {
    "Bulan": "Maret",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "April",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "Mei",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "Juni",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "Juli",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "Agustus",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "September",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "Oktober",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "November",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  },
  {
    "Bulan": "Desember",
    "Total": "0",
    "Langsung": "0",
    "Delivery": "0",
    "Dropship": "0"
  }
]',true);

$output=array();
foreach($data as $entry)
foreach($entry as $k=>$v)
{
if(!is_array($output[$k]))
$output[$k]=array();
$output[$k][]=$v;
}

print_r($output);
echo str_replace('],',"],\n",json_encode($output));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download