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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探