Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 foreach循环的结果存储为数组_Php_Arrays_Foreach - Fatal编程技术网

将php foreach循环的结果存储为数组

将php foreach循环的结果存储为数组,php,arrays,foreach,Php,Arrays,Foreach,我使用foreach循环生成一组缩略图链接。我正在使用Wordpress,出于这样或那样的原因,我的PHP执行的位置不是我想要呈现列表的位置。所以我的问题是:我是否可以用一个可以存储每个图像生成的所有html的东西来替换echo语句,而不仅仅是最后一个,并允许我在同一页的下一页生成它 谢谢你的帮助。以下是迄今为止我的php: foreach ($gallery_images as $galleryID) { $attachment = get_post( $galleryID );

我使用foreach循环生成一组缩略图链接。我正在使用Wordpress,出于这样或那样的原因,我的PHP执行的位置不是我想要呈现列表的位置。所以我的问题是:我是否可以用一个可以存储每个图像生成的所有html的东西来替换echo语句,而不仅仅是最后一个,并允许我在同一页的下一页生成它

谢谢你的帮助。以下是迄今为止我的php:

foreach ($gallery_images as $galleryID) {
    $attachment = get_post( $galleryID );

    $thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' );    //thumbnail src
    $full_img = wp_get_attachment_image_src( $galleryID, 'full' );  //full img src

    echo '<a href="' . $full_img[0] . '" id="description-button-' . $gallery_images_count . '" class="thumbLink" target="_blank"><img src="' . $thumb_img[0] .'"></a>';

    $gallery_images_count++;

}//end forEach

您可以将结果存储到数组中,以便以后可以回显结果:

$links = array();
foreach ($gallery_images as $galleryID) {
    $attachment = get_post( $galleryID );

    $thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' );    //thumbnail src
    $full_img = wp_get_attachment_image_src( $galleryID, 'full' );          //full img src

    $links[] = '<a href="' . $full_img[0] . '" id="description-button-' . $gallery_images_count . '" class="thumbLink" target="_blank"><img src="' . $thumb_img[0] .'"></a>';

    $gallery_images_count++;

}

而不是回声。。。$html[]=…?太好了,非常感谢您的帮助。出于兴趣,内爆会做什么?它将数组中的元素组合成一个大字符串,并用分隔符分隔第一个参数。文件:
$links = array();
foreach ($gallery_images as $galleryID) {
    $attachment = get_post( $galleryID );

    $thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' );    //thumbnail src
    $full_img = wp_get_attachment_image_src( $galleryID, 'full' );          //full img src

    $links[] = '<a href="' . $full_img[0] . '" id="description-button-' . $gallery_images_count . '" class="thumbLink" target="_blank"><img src="' . $thumb_img[0] .'"></a>';

    $gallery_images_count++;

}
echo implode("\n", $links);