将Mysql结果映射到自定义Json格式

时间:2017-02-07 15:54:23

标签: php mysql arrays json

我正在尝试将mysql结果映射到json编码,但结果并不像我想要的那样。

来自Mysql查询的结果

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
        )

)

如果我使用 echo json_encode($data); 结果如下:

[
  {
    "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"
  }
]

我的预期赞这样:

{
        "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]
      }

有可能吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

有很多方法..没有测试任何东西,我认为它应该看起来像这样

$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);

我得到的输出是以下

{"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"]}.

使用的完整代码:

<?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));