Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.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_Loops_Multidimensional Array - Fatal编程技术网

PHP-循环和多维数组

PHP-循环和多维数组,php,arrays,loops,multidimensional-array,Php,Arrays,Loops,Multidimensional Array,我在这里读了很多其他的帖子,但确实解决了我的问题。我正在访问MySQL数据库中的数据:图像ID和图像名称。我可以通过以下方式轻松获取信息: while($row = mysqli_fetch_array( $result )) { echo $row['name']; } 我在这里获取一个图像名称,我需要编写一个循环,将图像插入另一个数组中,如下所示: $newItem = array( //stuff like name, id, tax id etc. //... //, 'ima

我在这里读了很多其他的帖子,但确实解决了我的问题。我正在访问MySQL数据库中的数据:图像ID和图像名称。我可以通过以下方式轻松获取信息:

while($row = mysqli_fetch_array( $result )) {

echo $row['name'];

}
我在这里获取一个图像名称,我需要编写一个循环,将图像插入另一个数组中,如下所示:

$newItem = array(

//stuff like name, id, tax id etc.
//...
//,

'images' => array(
    array(
        'link' => 'http://www.example.com/images/image1.jpg',
        'description' => 'My image',

         ),

    array(
        'link' => 'http://www.example.com/images/image2.jpg',
        'description' => 'My Image 2',
         ),

//maybe more images here ...

    ),
我第一次尝试解决这个问题是将获取的数据放入一个变量中:

while($row = mysqli_fetch_array( $result )) {
    // DB access - > $row['name']

    $images .=
    array(
        'link'        => $imguri.$row['name'], //$imguri is the server link
        'description' => 'My image', //not important


         ).','; //with or without comma -> no success
    }
'images' => array($images),
然后插入变量:

while($row = mysqli_fetch_array( $result )) {
    // DB access - > $row['name']

    $images .=
    array(
        'link'        => $imguri.$row['name'], //$imguri is the server link
        'description' => 'My image', //not important


         ).','; //with or without comma -> no success
    }
'images' => array($images),

但这是行不通的。有人有主意吗?

你把字符串和数组搞混了。只需创建一个空数组,并在循环中向其添加元素:

$images = array();
while($row = mysqli_fetch_array( $result )) {

    $images[] =
    array(
        'link'        => $imguri.$row['name'], //$imguri is the server link
        'description' => 'My image', //not important    
         );
}

//
'images' => $images;
试试这个:

<?php
$newItem = array();

while($row = mysqli_fetch_array( $result ))
{
    $newItem['images'][] = array(
        'link'        => $imguri.$row['name'],
        'description' => 'My image'
    );
}

var_dump($newItem);

在while循环中使用php数组_push方法。 $images['images']=数组; 而$row=mysqli\u fetch\u array$result{ $image= 大堆 'link'=>$imguri.$row['name'], “描述”=>“我的图像”, ; 数组_push$images['images',$image;
}

var_dump向我显示:array1{[images]=>array0{}-不幸的是它不起作用你是说var_dump$images['images'];?对请检查您的查询是否返回任何输出。谢谢您的帮助。我更改了DB查询,现在它对我有效。@防震请详细说明“不起作用”-也许可以用您当前的、不起作用的代码更新您的问题,并包括您因不够清楚而收到的错误消息:当我打印命令变量转储数组时,它们总是空的。