无法在一个数组中转换两个表PHP

时间:2016-01-05 12:04:31

标签: php json

我希望格式看起来像这样:

{
"result": [
    {
            "Owner_ID": "1",
            "Owner_Name": "King",
            "Owner_IC": "997788-01-9267",
            "Owner_Contact": "012-34567890",
            "Owner_AccNum" : "124914848238902",
            "Car": {
                "Car_ID": "1",
                "Car_Name": "Aston Martin",
                "Car_Plate": "P 1"
                "Car_Color": "Red",
                "Car_Hour": "6",
                "Car_Day": "150",
            },
            {
                "Car_ID": "2",
                "Car_Name": "Sonata",
                "Car_Plate": "S 1234"
                "Car_Color": "Red",
                "Car_Hour": "10",
                "Car_Day": "200",
            }
    },
    {
            "Owner_ID": "2",
            "Owner_Name": "Dragon",
            "Owner_IC": "962738-98-8345",
            "Owner_Contact": "019-86427613",
            "Owner_AccNum" : "124914848238902",
            "Car": {
                "Car_ID": "3",
                "Car_Name": "Lambo",
                "Car_Plate": "L 104"
                "Car_Color": "Blue",
                "Car_Hour": "9",
                "Car_Day": "180",
            }

这是我的代码:

<?php
  define('HOST','host');
  define('USER','username');
  define('PASS','password');
  define('DB','database');

  $con = mysqli_connect(HOST,USER,PASS,DB);

  $sqls = "SELECT a.*, b.* FROM Car a, Owner b WHERE a.Owner_ID = b.Owner_ID";
  $ress = mysqli_query($con, $sqls);
  $results = array();
  if(! $ress )
  {
      die('Could not get data: ' . mysql_error());
  }
  while($row = mysqli_fetch_array($ress)){
    array_push($results,
    array('Owner_ID'=>$row[0],
          'Owner_Name'=>$row[1],
          'Owner_IC'=>$row[2],
          'Owner_Contact'=>$row[3],
          'Owner_AccNum'=>$row[4],
          'Owner_Password'=>$row[5],
          'Car_ID'=>$row[6],
          'Car_Name'=>$row[7],
          'Car_Plate'=>$row[8],
          'Car_Color'=>$row[9],
          'Car_Hour'=>$row[10],
          'Car_Day'=>$row[11],
       ));
     }
     echo json_encode(array("result"=>$results));

     mysqli_close($con);
    ?>

我不知道要改变什么以及如何制作成功代码以使其看起来像那种格式。我有一个错误“无法获取数据:”。 抱歉我的英文不好

2 个答案:

答案 0 :(得分:0)

array_push($results,
array('Owner_ID'=>$row[0],
      'Owner_Name'=>$row[1],
      'Owner_IC'=>$row[2],
      'Owner_Contact'=>$row[3],
      'Owner_AccNum'=>$row[4],
      'Owner_Password'=>$row[5],

   ));



  $results['car'] = array( 'Car_ID'=>$row[6],
      'Car_Name'=>$row[7],
      'Car_Plate'=>$row[8],
      'Car_Color'=>$row[9],
      'Car_Hour'=>$row[10],
      'Car_Day'=>$row[11],
      );

以这种方式使用它将起作用

答案 1 :(得分:0)

试试这件事它会起作用

    $results = array();
while ($row = mysqli_fetch_array($ress)) {
    $index = array_search($row[0], array_column($results, 'Owner_ID'));
    if ($index !== FALSE) {
        $results[$index]['Car'][] = array(
            'Car_ID' => $row[6],
            'Car_Name' => $row[7],
            'Car_Plate' => $row[8],
            'Car_Color' => $row[9],
            'Car_Hour' => $row[10],
            'Car_Day' => $row[11]
        );
    } else {
        $results[] = array('Owner_ID' => $row[0],
            'Owner_Name' => $row[1],
            'Owner_IC' => $row[2],
            'Owner_Contact' => $row[3],
            'Owner_AccNum' => $row[4],
            'Owner_Password' => $row[5],
            'Car' => array(
                array(
                    'Car_ID' => $row[6],
                    'Car_Name' => $row[7],
                    'Car_Plate' => $row[8],
                    'Car_Color' => $row[9],
                    'Car_Hour' => $row[10],
                    'Car_Day' => $row[11]
                )
            )
        );
    }