我有一个非常简单的Wordpress while循环:
$loop = new WP_Query( args ) );
while ( $loop->have_posts() ) : $loop->the_post();
$data = grab_something( args );
echo $data;
echo "<br/";
endwhile; 这给了我一些类似的东西:
datastring1
datastring2
anotherdata
somethingelse
and else
and so forth
(...)我想将while循环中的这些值保存为数组或变量,例如。
$data1 = "datastring1";
$data2 = "datastring2";
$data3 = "anotherdata";
(...)如何操作?:)
谢谢!
发布于 2011-04-15 23:32:17
使用计数器$i跟踪数字,然后可以将结果保存为一个数组或一组变量。
$loop = new WP_Query( args ) );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$data = grab_something( args );
$i++;
$array[] = $data; // Saves into an array.
${"data".$i} = $data; // Saves into variables.
endwhile; 如果使用第二种方法,则只需使用$i计数器。如果您使用上述语法保存到数组中,则会自动生成索引。
发布于 2011-04-15 23:29:39
您可以轻松地保存在数组中
$array=array();
while ( $loop->have_posts() ) : $loop->the_post();
$data = grab_something( args );
$array[]=$data;
endwhile; print_r($data);
循环迭代时,$array将存储从索引0到元素数量的数据
https://stackoverflow.com/questions/5679004
复制相似问题