我有一个页面,将需要为每个文章生成缩略图图像。每个缩略图的数量会有所不同,所以我想检查数据库中的文章,并为所有缩略图创建一个数组,但我在逻辑上有些困难。
这就是我到目前为止所知道的:
for ($i=1; $i<20; $i++) {
$thumbImages = array(
'src' => $newblogDoc['tvs']['thumbnail-image-' . [$i]]
);
}这方向对吗?有没有更有效的方法将其放入数组中?
发布于 2012-07-05 22:44:34
你的问题不是很清楚,但是如果你想创建一个数组的关联数组,那么你需要这样做:
for( $i=1; $i<20; $i++){
$thumbImages[] = array(
'src' => $newblogDoc['tvs']['thumbnail-image-'.[$i]]
);
}发布于 2012-07-05 22:43:59
您现在拥有的代码每次都会用一个新的数组覆盖$thumbImages。您要做的是在循环之前创建一个新的数组,然后在循环内附加到该数组。因此:
$thumbImages = array();
for ($i=1; $i<20; $i++){
$thumbImages[] = $newblogDoc['tvs']['thumbnail-image-'.[$i]];
}https://stackoverflow.com/questions/11346653
复制相似问题