doujiong9915 2018-12-02 02:27
浏览 57
已采纳

如何使用php json创建适当的维数组

i have two table, Car and Vendor

table: Car
--------------------------- 
| carID | carBrand        |
---------------------------
| 1     | Volvo           |
---------------------------
| 2     | BMW             |
---------------------------
| 3     | Mercedes        |
---------------------------

table: Vendor
----------------------------------------------------
| vendorID | vendorName        | telephone | carID |
----------------------------------------------------
| 1        | Jusoh Trading     | 12345     | 1     |
----------------------------------------------------
| 2        | Ibrahim and Sons  | 34566     | 1     |
----------------------------------------------------
| 3        | GMC Auto          | 566444    | 2     |
----------------------------------------------------
| 4        | Perodua Services  | 456666    | 3     |
----------------------------------------------------
| 5        | AMG Specs         | 5666      | 3     |
----------------------------------------------------

im using sql query (codeigniter) to produce output using json. By using the output, i will create a chart which is my plugins required array format. My success output using my above sql & json encode is as below:

[
  {
    "carBrand": "Volvo",
    "carID": "1"
  },
  [
    {
      "vendorID": "1",
      "vendorName": "Jusoh Trading"
    },
    {
      "vendorID": "2",
      "vendorName": "Ibrahim and Sons"
    }
  ],
  {
    "carBrand": "BMW",
    "carID": "2"
  },
  [
    {
      "vendorID": "3",
      "vendorName": "GMC Auto"
    }
  ],
  {
    "carBrand": "Mercedes",
    "carID": "3"
  },
  [
    {
      "vendorID": "4",
      "vendorName": "Perodua Services"
    },
    {
      "vendorID": "5",
      "vendorName": "AMG Specs"
    }
  ]
]

my code to generate the output:

$data = array();
$this->db->select("carBrand, carID");
$this->db->from('car');
$query = $this->db->get();

$result = $query->result();

foreach ($result as $row):
    $data[] = $row;

    $this->db->select("vendorID, vendorName");
    $this->db->from('vendor');
    $this->db->where('carID',$row->carID);
    $query = $this->db->get();
    $res = $query->result();

    $child = array();
        foreach ($res as $val):
            $child[] = $val;
        endforeach;

    array_push($data, $child);
endforeach;

echo json_encode($data);
exit;

But my problem is, the map plugins required a bit different format. I need to change a little bit to my output format. The exactly format i need as below:

[
  {
    "carBrand": "Volvo",
    "vendorlist": [
    {
        "vendorID": "1",
        "vendorName": "Jusoh Trading"
    },
    {
        "vendorID": "2",
        "vendorName": "Ibrahim and Sons"
    }
    ]
  },
  {
    "carBrand": "BMW",
    "vendorlist": [
    {
        "vendorID": "3",
        "vendorName": "GMC Auto"
    }
    ]
  }
  # .. other data .. #
];

i need to remove "carID" and add 'vendorlist' .. i need suggestion to modify my code. thank you

</div>
  • 写回答

1条回答 默认 最新

  • douzhonglong3789 2018-12-02 03:42
    关注
    $json = '[
      {
        "carBrand": "Volvo",
        "carID": "1"
      },
      [
        {
          "vendorID": "1",
          "vendorName": "Jusoh Trading"
        },
        {
          "vendorID": "2",
          "vendorName": "Ibrahim and Sons"
        }
      ],
      {
        "carBrand": "BMW",
        "carID": "2"
      },
      [
        {
          "vendorID": "3",
          "vendorName": "GMC Auto"
        }
      ],
      {
        "carBrand": "Mercedes",
        "carID": "3"
      },
      [
        {
          "vendorID": "4",
          "vendorName": "Perodua Services"
        },
        {
          "vendorID": "5",
          "vendorName": "AMG Specs"
        }
      ]
    ]';
    
    $narr = [];
    $arr = json_decode($json,true);
    
    foreach($arr as $k => $v)
    {
    
      if( $k%2 == 0)
      {
        $narr[] = ['carBrand' => $v['carBrand'] , 'vendorlist' =>  $arr[$k+1]];
      }
    }
    
    $new_json = json_encode($narr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!