Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从一到多mysql结果创建多维数组_Php_Mysql_Arrays - Fatal编程技术网

Php 从一到多mysql结果创建多维数组

Php 从一到多mysql结果创建多维数组,php,mysql,arrays,Php,Mysql,Arrays,我有三个mysql表:clients、cars和insurance。每个客户可以有多辆车,每辆车可以有多份保险单。以下是我的查询和SQLFIDLE的链接: SELECT a.*, b.id AS car_id, b.license_number, b.brand, b.model, c.id AS insurance_id, c.insurance_company, c.start_date, c.end_date, c.price FROM clients a LEFT JOIN

我有三个mysql表:
clients
cars
insurance
。每个客户可以有多辆车,每辆车可以有多份保险单。以下是我的查询和SQLFIDLE的链接:

SELECT
  a.*,
  b.id AS car_id, b.license_number, b.brand, b.model,
  c.id AS insurance_id, c.insurance_company, c.start_date, c.end_date, c.price
FROM clients a
LEFT JOIN cars b ON a.id = b.client_id
LEFT JOIN insurance c ON b.id = c.car_id

因此,对于两个客户机,其中第一个客户机有两辆车和三份保险单,而第二个客户机没有车也没有保险,查询将返回4行

我不明白的是,如何循环此查询以返回以下结构,同时避免重复的客户端条目

Array
(
  [0] => Array
    (
      [id] => 1
      [first_name] => John
      [last_name] => Smith
      [cars] => Array
        (
          [id] => 1
          [license_number] => 'plate1'
          [brand] => 'BMW'
          [model] => 'E35'
          [insurance] => Array
            (
              [id] => 1
              [start_date] => '2015-02-10'
              [end_date] => '2016-02-10'
              [price] => '100'
            )

            (
              [id] => 2
              [start_date] => '2014-02-10'
              [end_date] => '2015-02-10'
              [price] => '50'
            )
        )

        (
          [id] => 2
          [license_number] => 'plate2'
          [brand] => 'VW'
          [model] => 'Golf'
          [insurance] => Array
            (
              [id] => 3
              [start_date] => '2015-02-10'
              [end_date] => '2016-02-10'
              [price] => '100'
            )
        )
    )

  [1] => Array
    (
      [id] => 1
      [first_name] => John
      [last_name] => Smith
      [cars] => NULL
    )
)

我不打算为您写出所有必要的字段,但它实际上只涉及一个fetch循环和一些构建子数组的
if()
逻辑:

$info = array()
while($row = fetch from db) {
    if(!isset($info[$row['userID']]) {
         $info[$row['userID']] = array(
              'first_name' => $row['first_name'],
              ...
              'cars' => array()
         );
    }
    if (!isset($info[$row['userID']]['cars'][$row['carID']]) {
         etc...
    }
}
对于放入的每个子数组,您只需要另一个
if()
测试来初始化该子数组,然后从您获取的
$row
填充它


当您迭代行时,您自然会构建您的结构。

这很容易。它是关于使用assoc阵列的。您可以使用id、汽车id和保险id作为数组键

因此,假设得到的结果是存储在$rows变量中的获取的assoc行。简单的循环将是:

$result = array();
foreach($rows as $row) {
    $id    = $row['ID'];
    $carId = $row['CAR_ID'];
    $insId = $row['INSURANCE_ID'];

    $result[$id]['first_name'] = $row['FIRST_NAME'];
    $result[$id]['last_name']  = $row['LAST_NAME'];

    if(!isset($result[$id]['cars'])) {
        $result[$id]['cars'] = array();
    }

    if($carId && !isset($result[$id]['cars'][$carId])) {
        $car = array(
            'id'             => $row['CAR_ID'],
            'license_number' => $row['LICENSE_NUMBER'],
            'brand'          => $row['BRAND'],
            'model'          => $row['MODEL'],
            'insurance'      => array()
        );
        $result[$id]['cars'][$carId] = $car;
    }

    if($insId && isset($result[$id]['cars'][$carId]) && !isset($result[$id]['cars'][$carId]['insurance'][$insId])) {
        $insurance = array(
            'id'         => $row['INSURANCE_ID'],
            'start_date' => $row['START_DATE'],
            'end_date'   => $row['END_DATE'],
            'price'      => $row['PRICE']
        );
        $result[$id]['cars'][$carId]['insurance'][$insId] = $insurance;
    }
}

人们经常问这种问题。但它真的很简单:循环。有没有办法将值(客户、汽车、保险)作为数组而不是对象返回,并对数组进行数字索引?@Norbert实际上在我的示例中也有一个数组,是的,让它们进行数字自动索引而不是使用自己的键并不困难。您只需另外迭代已添加的汽车和保险项目,并检查相应的id值,以确保您要添加的项目尚未存在。