PHP为每个循环创建不相等的数组
我正在尝试使用PHP将MySQL表转换为数组。图像表中的每个列表ID至少有一个图像ID,但是图像ID的总数可能会有所不同 图像表:PHP为每个循环创建不相等的数组,php,mysql,Php,Mysql,我正在尝试使用PHP将MySQL表转换为数组。图像表中的每个列表ID至少有一个图像ID,但是图像ID的总数可能会有所不同 图像表: |Listing ID | Image ID | | 1234 | 1 | | 1234 | 2 | | 1234 | 3 | | 1235 | 1 | | 1235 | 2 | 本质上,我想将图像表转换为一个具有键值对的数组,如下所示: arr
|Listing ID | Image ID |
| 1234 | 1 |
| 1234 | 2 |
| 1234 | 3 |
| 1235 | 1 |
| 1235 | 2 |
本质上,我想将图像表转换为一个具有键值对的数组,如下所示:
array([0] 1234 => /FilePath/1234-1::/FilePath/1234-2::/FilePath/1234-3, [1] 1235 => /FilePath/1235-1::/FilePath/1235-2, ...)
我已经能够编写一些代码,在很大程度上完成了这项任务。然而,也存在一些问题
我的代码的主要问题是$lid数组和$prod数组包含的元素数量不同($prod比$lid多包含一个元素)。这让我感到困惑,因为它们从同一个映像表中提取数据,因此应该具有相同数量的元素
我的代码:
//Set document path
$target_path = realpath($_SERVER['DOCUMENT_ROOT']). "\mypath\image\uploads\\";
//Query to download all listing IDs in Markers table
$query = $db->query("SELECT * FROM image");
while ($row = $query->fetch(PDO::FETCH_BOTH)) {
$temp[] = $row;
}
foreach($temp as $i=>$v){
$line = $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
if($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID']){
//appending '::' to each string in the $prod array
$prod[] = $line."::";
}else{
// * will serve as the delimiter in the $prod array
$prod[] = $line."*";
//Add each listing ID into Listing Array
$lid[] = $v['L_ListingID'];
}
}
//Convert the array into a big string
$bigString = implode("", $prod);
//Chop up the big string into sections delimited by '*' and insert into 'prod' array
$prod = explode("*",$bigString);
//Combine $lid array with $prod array
$combo = array_combine($lid, $prod);
第二个问题是,每当运行foreach循环时,PHP都会返回以下消息:
注意:第78行C:\mypath\getimage.php中未定义的偏移量:2789
第2789行是image表中的最后一行,因此我认为此错误通知可能与$lid和$prod的元素数相差1有关
任何建议都将不胜感激。另外,如果你能想出一个更有效的方法来完成这项任务,请告诉我
谢谢,解答此问题:
注意:第78行C:\mypath\getimage.php中未定义的偏移量:2789
这是因为你正在做:
if($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID']){
即使您的foreach
位于最后一个元素上,您也需要$i+1
您需要检查该$i+1
键是否存在,替换为:
if(array_key_exists($i+1, $temp) && ($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID'])){
对于“main”问题,您的代码太复杂了。如果您开始抖动字符串和数组,则说明有问题。只使用字符串或数组,但不能同时使用两者,这将更难维护
$bigstring = '';
$lid = array()
foreach($temp as $i=>$v){
$bigstring .= $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
if(array_key_exists($i+1, $temp) && ($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID'])) {
$bigstring .= "::";
}else{
$bigstring .= "*";
}
if (!in_array($v['L_ListingID'], $lid)) {
$lid[] = $v['L_ListingID'];
}
}
最后一件事:在对变量使用数组运算符之前初始化数组是一个很好的做法
$temp = array();
while ($row = $query->fetch(PDO::FETCH_BOTH)) {
$temp[] = $row;
}
否则,PHP将抛出E_通知。对于问题:
注意:第78行C:\mypath\getimage.php中未定义的偏移量:2789
这是因为你正在做:
if($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID']){
即使您的foreach
位于最后一个元素上,您也需要$i+1
您需要检查该$i+1
键是否存在,替换为:
if(array_key_exists($i+1, $temp) && ($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID'])){
对于“main”问题,您的代码太复杂了。如果您开始抖动字符串和数组,则说明有问题。只使用字符串或数组,但不能同时使用两者,这将更难维护
$bigstring = '';
$lid = array()
foreach($temp as $i=>$v){
$bigstring .= $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
if(array_key_exists($i+1, $temp) && ($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID'])) {
$bigstring .= "::";
}else{
$bigstring .= "*";
}
if (!in_array($v['L_ListingID'], $lid)) {
$lid[] = $v['L_ListingID'];
}
}
最后一件事:在对变量使用数组运算符之前初始化数组是一个很好的做法
$temp = array();
while ($row = $query->fetch(PDO::FETCH_BOTH)) {
$temp[] = $row;
}
否则,PHP将抛出E_通知。使用此算法,您必须对最后一行进行特殊处理(这将导致通知“未定义偏移量”,如图所示) 以下算法应构建您期望的阵列:
$combo = array();
foreach($temp as $v){
$line = $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
$key = $v['L_ListingID'];
if (array_key_exists($key, $combo)) {
// Append
$combo[$key] .= '::' . $line;
} else {
// Create key
$combo[$key] = $line;
}
}
使用此算法,您必须为最后一行设置一个特殊情况(这将导致通知“Undefined offset”,如图所示) 以下算法应构建您期望的阵列:
$combo = array();
foreach($temp as $v){
$line = $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
$key = $v['L_ListingID'];
if (array_key_exists($key, $combo)) {
// Append
$combo[$key] .= '::' . $line;
} else {
// Create key
$combo[$key] = $line;
}
}
此代码在$prod和$lid之间生成的元素数量不等。此代码在$prod和$lid之间生成的元素数量不等。