Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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更改项_Php_Arrays - Fatal编程技术网

来自数据库的数组中的PHP更改项

来自数据库的数组中的PHP更改项,php,arrays,Php,Arrays,我有这样一个数组: [0] => Array( [id] => 1602[title] => Title [image] => 140061419150image.jpg ) 我从一个数据库生成这个数组,如下所示: $results = array(); while($row = mysqli_fetch_assoc($query)){ /*if($row['imag

我有这样一个数组:

[0] => Array(
        [id] => 1602[title] => Title [image] => 140061419150image.jpg
    )
我从一个数据库生成这个数组,如下所示:

$results = array();
                while($row = mysqli_fetch_assoc($query)){
                        /*if($row['image']){
                                $results[] = 'http://www.website.com/images/' . $row['image'];
                        }else{*/
                                $results[] = $row;
                        //}
                }
我现在要做的是,当这些数据进入并被添加到数组中时,如果是它的图像,我想添加到它的完整URL。我尝试了上面的注释,它只返回图像,而不返回任何其他数据(id和标题)。我也尝试了$row=='image',但它返回所有数据,但添加了url:(


我做错了什么?

在一次迭代中,您正在检查它是否是图像。如果是图像,则将图像的完整路径添加到数组中,并忽略
$row
的其余部分。若要解决此问题,请删除
否则

while($row = mysqli_fetch_assoc($query)){
     if (!empty($row['image'])) { // check if there is set image for current row.
        $row['image'] = "http://www.website.com/images/{$row['image']}";
     }

     $results[] = $row;
}

它只返回图像,因为这是您要求的全部信息。如果您想要所有信息,则需要专门调用它。请尝试以下操作:$results=array();while($row=mysqli_fetch_assoc($query)){if(!empty($row['image']){$results[]='.$row['image'];}否则{$results[]=$row;}非常感谢Justinas…我不知道为什么我有这么多的反对票:(