PHP for each 循环创建不相等的数组

标签 php mysql

我正在尝试使用 PHP 将 MySQL 表转换为数组。图像表中的每个列表 ID 至少有一个图像 ID,但是图像 ID 的总数可能会有所不同。

图像表:

|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 都会返回以下消息:

Notice: Undefined offset: 2789 in C:\mypath\getimage.php on line 78

第2789行是image表的最后一行,所以我认为这个错误提示可能与$lid和$prod的元素数量相差1有关。

非常感谢任何建议。另外,如果您能想到一种更有效的方法来完成此任务,请告诉我。

谢谢,

最佳答案

对于问题:

Notice: Undefined offset: 2789 in C:\mypath\getimage.php on line 78

这是因为你在做:

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'])){

对于“主要”问题,您的代码太复杂了。如果你开始摇动字符串和数组,那就有问题了。仅使用字符串或仅使用数组,但不要同时使用两者,这将更难维护。

$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_NOTICE。

关于PHP for each 循环创建不相等的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077272/

相关文章:

javascript - e.preventDefault 不停止表单上的重定向

javascript - 使用 ng-click 发布表单(旧方式),这是不好的做法吗?

php - 在使用 curl 时,出于安全原因,如何使我的 cacert.pem 保持最新状态?

mysql - 使用不同的数据库执行单个查询。 Rails 上的 Ruby

php - 如何在 PHP 中只显示一次评论但显示来自 mysql 的所有图像?

java - java、.net、php 或 rails 中的 Web 应用程序?

mysql - 使用 Lua 连接到 MySQL 数据库

mysql - 错误 1049 (42000) : Unknown database 'tweetdata' Mysql

php - 需要帮助使 sql 查询按列比较数据

PHP Mail 表单不在 Linux 服务器上发送电子邮件