snipt

Ctrl+h for KB shortcuts

PHP

Inizializzare un array con un dato range e mescolarlo

1
2
3
4
5
6
7
            <?php
$bannerArray = range(1,8);
shuffle($bannerArray);
foreach ($bannerArray as $numBanner) {
    echo '<img id="banner-img" src="../img/slideshow/'.$numBanner.'.jpg">'; echo "\n";
}
?>
https://snipt.net/embed/988c6162a28f1b43d78a07eebf239d9e/
/raw/988c6162a28f1b43d78a07eebf239d9e/
988c6162a28f1b43d78a07eebf239d9e
php
PHP
7
2019-07-31T06:26:34
True
False
False
/api/public/snipt/41732/
inizializzare-un-array-con-un-dato-range-e-mescolarlo
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x"> </span><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$bannerArray</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">8</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="nb">shuffle</span><span class="p">(</span><span class="nv">$bannerArray</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="k">foreach</span> <span class="p">(</span><span class="nv">$bannerArray</span> <span class="k">as</span> <span class="nv">$numBanner</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">echo</span> <span class="s1">&#39;&lt;img id=&quot;banner-img&quot; src=&quot;../img/slideshow/&#39;</span><span class="o">.</span><span class="nv">$numBanner</span><span class="o">.</span><span class="s1">&#39;.jpg&quot;&gt;&#39;</span><span class="p">;</span> <span class="k">echo</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span><span id="L-7"><a name="L-7"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
array, shuffle