内爆字符串并插入数组?
I can't get the implode function to work with my array. I'm building a site and every time you reload the page the background image is randomly chosen.
I have a loop with different image url:s like this:
<?php
if( have_rows('pictures', 'option') ):
while ( have_rows('pictures', 'option') ) : the_row();
$pictures[] = get_sub_field('picture');
$picturesimploded = "'" . implode("', '", $pictures) . "'";
endwhile;
endif;
?>
Below is the code to randomize which url is chosen:
<?php
$bg = array( $picturesimploded ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
The url is then applied to a div:
<div style="background-image: url( <?php echo $selectedBg; ?> );">
The output however prints all links:
<div style="background-image: url( 'http://example.com/image1', 'http://example.com/image1', 'http://example.com/image1' );">
It seems like the array can't separate the arrays. When I insert the links manually, directly, in the array like this it works:
<?php
$bg = array( 'http://example.com/image1', 'http://example.com/image1', 'http://example.com/image1' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
Any ideas how to get the randomizing to work?
我无法使用implode函数来处理我的数组。 我正在建立一个网站,每次你重新加载页面时,随机选择背景图像。 p>
我有一个不同图像的循环网址:这样: strong > p>
以下是随机选择哪个网址的代码: strong> p>
然后将网址应用于div: strong>
然而输出打印所有链接: strong> p>
任何想法如何让随机化工作? p>
&lt;?php
if(has_rows('pictures','option')):
while(have_rows('pictures','option')): the_row();
$ pictures [] = get_sub_field('picture');
$ picturesimploded =“'”。 implode(“','”,$ pictures)。 “';;
endwhile;
endif;
?&gt;
code> pre>
&lt;?php
$ bg = array($ picturesimploded); //文件名数组
$ i = rand(0,count($ bg)-1); //生成数组的随机数大小
$ selectedBg =“$ bg [$ i]”; //设置变量等于选择了哪个随机文件名
?&gt;
code> pre>
&lt; div style =“background-image:url(&lt;?php echo $ selectedBg;?&gt;);”&gt;
code> pre >
&lt; div style =“background-image:url('http ://example.com/image1','http://example.com/image1','http://example.com/image1');“&gt;
code> pre>
似乎数组无法分离数组。 当我直接在这样的数组中手动插入链接时,它可以工作: p>
&lt;?php
$ bg = array('http://example.com / image1','http://example.com/image1','http://example.com/image1'); //文件名数组
$ i = rand(0,count($ bg)-1); //生成数组的随机数大小
$ selectedBg =“$ bg [$ i]”; //设置变量等于选择了哪个随机文件名
?&gt;
code> pre>
replace
$bg = array( $picturesimploded );
with
$bg = explode( $picturesimploded );
-- when you call
$bg = array( $picturesimploded );
you are making an array with one entry like this:
[0] => 'image,image,image,image,image'
when you use explode it will be like this
[0] => image,
[1] => image,
etc
an alternative would be to do this:
<?php
$pictures = array();
if( have_rows('pictures', 'option') ):
while ( have_rows('pictures', 'option') ) : the_row();
$pictures[] = get_sub_field('picture');
endwhile
endif;
$i = rand(0, count($pictures)-1); // generate random number size of the array
$selectedBg = $pictures[$i]; // set variable equal to which random filename was chosen
?>
You have a string rather than a set of elements in your array constructor, separate these URLs out with explode:
$bg = explode(',', $picturesimploded);
- flip the array to work on values.
- use array_rand with 1 to get 1 random element
-
Example: http://ideone.com/cpV2Va
<?php $pictures = array(); if( have_rows('pictures', 'option') ): while ( have_rows('pictures', 'option') ) : the_row(); $pictures[] = get_sub_field('picture'); endwhile endif; $selectedBg = array_rand(array_flip($pictures), 1);