用PHP Mysql制作一个3d数组

时间:2012-09-02 17:06:32

标签: php mysql database arrays

我想从数据库创建一个包含多维数组的数组。该数据库有3个表,一个用于车辆,一个用于损坏,一个用于损坏照片。

表格工具有两列 ID 名称

表损害有四列 damageID vehicleID damageType damageDescription

表损坏照片有三列 damagePhotoID damageID damagePhotoUrl

我需要将thoose三列组合成一个数组,如下所示:

$vehicle = array( 
"id" => "somestring", 
"name" => "somestring", 
"damages" => array( 
    "damagesType" => "somestring", 
    "damagesDescription" => "somestring", 
    "photoOfDamages" => array( 
            "damagesPhotoUrl" => "somestring" 
    ) 
) 
); 

我正在使用此代码:

$query = "SELECT * from vehicle v LEFT JOIN damages d ON v.id = d.vehicleID LEFT JOIN   damagesPhotos p ON d.damagesID = p.damagesID WHERE d.damagesID = p.damagesID AND v.id = 1";

$result = mysql_query($query);

$i = 0;
$vehicle = array();
while($row = mysql_fetch_array($result)){
    $vehicle[$i] = array( 
        "id" => $row[id], 
        "name" => $row[name], 
        "damages" => array( 
            "damagesType" => $row[damagesType], 
            "damagesDescription" => $row[damagesDescription], 
            "photoOfDamages" => array( 
                    "damagesPhotoUrl" => $row[damagesPhotoUrl] 
        ) 
        )
  );
  $i++;
  }

然后它返回:

  [{"vehilceId":"1",
  "name":"AW55005",
  "damages":{
        "damagesType":"Exterior",
        "damagesDescription":"Rust",
        "photoOfDamages":{
              "damagesPhotoUrl":"link to damagesPhoto 01"
        }
  }
  },

  {"vehilceId":"1",
  "name":"AW55005",
  "damages":{
        "damagesType":"Exterior",
        "damagesDescription":"Rust",
        "photoOfDamages":{
              "damagesPhotoUrl":"link to damagesPhoto 02"
  }
  }
  },

  {"vehilceId":"1",
  "name":"AW55005",
  "damages":{
        "damagesType":"Interior",
        "damagesDescription":"Scratch",
        "photoOfDamages":{
              "damagesPhotoUrl":"link to damagesPhoto 03"
        }
  }
  }

但是你可以看到前两个对象是相同的只有damagePhotoUrl是不同的。如何合并thoose两个数组,使它看起来像这样:

{"vehilceId":"1",
"name":"AW55005",
"damages":{
    "damagesType":"Exterior",
    "damagesDescription":"Rust",
    "photoOfDamages":{
        {"damagesPhotoUrl":"link to damagesPhoto 01"},
        {"damagesPhotoUrl":"link to damagesPhoto 02"}
    }
}
}, ...

提前致谢。

/ Morten

2 个答案:

答案 0 :(得分:1)

理想情况下,我建议您将原始查询分解为三个单独的查询,然后构建关联数组,首先处理来自“Vehicle”表的结果,然后使用“damage”表和“damagePhotos”表格。

但是如果您仍然只想进行一次查询,那么这就是您的操作方法,请注意我已经修改了代码,以支持每辆车不止一次损坏,每次损坏不止一张照片:

$query = "SELECT * from vehicle v LEFT JOIN damages d ON v.id = d.vehicleID LEFT JOIN   damagesPhotos p ON d.damagesID = p.damagesID WHERE d.damagesID = p.damagesID AND v.id = 1";

$result = mysql_query($query);

$i = 0;
$vehicle = array();
while($row = mysql_fetch_array($result)){
    $id = $row['id'];
    $damageId  = $row['damageId'];
    $damagesPhotoId = $row['damagesId'];

    if (!isset($vehicle[$id])) {
        $vehicle[$id] =  array(
            'id' => $id,
            'name' => $row['name'],
            'damages' => array(
            )
        )
    }
    if (!isset($vehicle[$id]['damages'][$damageId])) {
        $vehicle[$id]['damages'][$damageId] = array(
            'damagesType' => $row['damagesType'],
            'damageDescription' = $row['damageDescription'],
            'photoOfDamages' => array ()
        )
    }
    $vehicle[$id]['damages'][$damageId]['photoOfDamages'][] = array(
        'damagesPhotoUrl' => $row['damagesPhotoUrl']
    )
}

答案 1 :(得分:1)

答案是你不能这样做:你想要的是:

"photoOfDamages":{
    {"damagesPhotoUrl":"link to damagesPhoto 01"},
    {"damagesPhotoUrl":"link to damagesPhoto 02"}
}

但是如果您尝试将具有相同键的值添加到关联数组中,它将覆盖最后一个值,这意味着,如果您要创建:

"photoOfDamages":{
    {"damagesPhotoUrl":"link to damagesPhoto 01"}
}

并尝试添加:

{"damagesPhotoUrl":"link to damagesPhoto 02"}

结果将是:

"photoOfDamages":{
    {"damagesPhotoUrl":"link to damagesPhoto 02"}
}

为了解决这个问题,您需要做的是更改key以连接正在运行的计数器,例如:

"photoOfDamages":{
    {"damagesPhotoUrl-1":"link to damagesPhoto 01"},
    {"damagesPhotoUrl-2":"link to damagesPhoto 02"}
}

对于完成工作的代码,以下是一个示例 - 为了使其适用于您,您应该取消注释,注释行并注释line with the foreach`:< / p>

<?php

$rows = array(
    array(//vehicle 1 
            "id" => "somestring",
            "name" => "somestring",
            "damages" => array( 
                "damagesType" => "somestring", 
                "damagesDescription" => "somestring", 
                "photoOfDamages" => array( 
                            "damagesPhotoUrl" => "somestring1")
        )
    ),
     array(//vehicle 2
            "id" => "somestring",
            "name" => "somestring",
            "damages" => array( 
                "damagesType" => "somestring", 
                "damagesDescription" => "somestring", 
                "photoOfDamages" => array( 
                            "damagesPhotoUrl" => "somestring2")
        )
    )
);
$i = 0;
//$row = mysql_fetch_array($result);
//while($row){
foreach($rows as $row){ //this should be commented
    if($id === $row["id"]){
        $j++;
        $vehicle[$i]["damages"]["photoOfDamages"]["damagesPhotoUrl-$j"] = $row["damages"]["photoOfDamages"]["damagesPhotoUrl"];          
        continue;
    }
    $j = 1;
    $i++;
    $id = $row["id"];
    $vehicle[$i] = array( 
        "id" => $id, 
        "name" => $row[name], 
        "damages" => array( 
            "damagesType" => $row["damages"]["damagesType"], 
            "damagesDescription" => $row["damages"]["damagesDescription"], 
            "photoOfDamages" => array( 
                    "damagesPhotoUrl-$j" => $row["damages"]["photoOfDamages"]["damagesPhotoUrl"]  
            ) 
        )
  );
  //$row = mysql_fetch_array($result);      
}
print_r($vehicle);
?>

<强>输出:

Array
(
    [1] => Array
        (
            [id] => somestring
            [name] => somestring
            [damages] => Array
                (
                    [damagesType] => somestring
                    [damagesDescription] => somestring
                    [photoOfDamages] => Array
                        (
                            [damagesPhotoUrl-1] => somestring1
                            [damagesPhotoUrl-2] => somestring2
                        )

                )

        )

)