Tolong pilih kategori sesuai, jenis posting (diskusi atau bukan) dan sertakan tag/topik yang sesuai seperti komputer, java, php, mysql, dll. Promosi atau posting tidak pada tempatnya akan kami hapus!
- Bagi Anda yang ingin mendaftar, baca link berikut:
http://diskusiweb.com/discussion/50491/how-to-registrasi-diskusiweb-com-baca-ini-terlebih-dahulu
- Cara menyisipkan kode program supaya tampil rapi dan terformat dengan baik di diskusiweb.com: http://www.diskusiweb.com/discussion/50415/cara-menyisipkan-kode-program-di-diskusiweb-com
- Cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

array_rand di Multidimensional Array

Para agan" saya ingin menampilkan random quotes.Jadi setiap reload page berubah quotes.
Saya menggunakan array multidimensi untuk quotesnya.

Ini code saya :<div class='geshi_syntax'><table><tr><td><div class='code'><pre class="php"><span class="kw2"><?php</span>
<span class="re0">$value</span><span class="sy0">=</span> <span class="kw3">array</span><span class="br0">(</span>
<span class="kw3">array</span><span class="br0">(</span>
<span class="st0">"name"</span> <span class="sy0">=></span> <span class="st0">"Linus Trovalds"</span><span class="sy0">,</span>
<span class="st0">"quote"</span> <span class="sy0">=></span> <span class="st0">"This is Linus Quotes"</span><span class="sy0">,</span>
<span class="st0">"title"</span> <span class="sy0">=></span> <span class="st0">"Founder Linux"</span>
<span class="br0">)</span><span class="sy0">,</span>
<span class="kw3">array</span><span class="br0">(</span>
<span class="st0">"name"</span> <span class="sy0">=></span> <span class="st0">"Bill Gates"</span><span class="sy0">,</span>
<span class="st0">"quote"</span> <span class="sy0">=></span> <span class="st0">"This is Bill Gates"</span><span class="sy0">,</span>
<span class="st0">"title"</span> <span class="sy0">=></span> <span class="st0">"Founder Microsoft"</span>
<span class="br0">)</span><span class="sy0">,</span>
<span class="kw3">array</span><span class="br0">(</span>
<span class="st0">"name"</span> <span class="sy0">=></span> <span class="st0">"Fandy Akhmad"</span><span class="sy0">,</span>
<span class="st0">"quote"</span> <span class="sy0">=></span> <span class="st0">"Stop Dreaming, Start Action"</span><span class="sy0">,</span>
<span class="st0">"title"</span> <span class="sy0">=></span> <span class="st0">"Junior Web Designer"</span>
<span class="br0">)</span>
<span class="br0">)</span><span class="sy0">;</span>
 
 
<span class="coMULTI">/*** perulangan array ***/</span>
<span class="kw1">foreach</span><span class="br0">(</span> <span class="re0">$value</span> <span class="kw1">as</span> <span class="re0">$item</span> <span class="br0">)</span>
<span class="br0">{</span>
<span class="kw1">echo</span> <span class="re0">$item</span><span class="br0">[</span><span class="st_h">'name'</span><span class="br0">]</span><span class="sy0">.</span><span class="st_h">'<br />'</span><span class="sy0">.</span><span class="re0">$item</span><span class="br0">[</span><span class="st_h">'quote'</span><span class="br0">]</span><span class="sy0">.</span><span class="st_h">'<br />'</span><span class="sy0">.</span><span class="re0">$item</span><span class="br0">[</span><span class="st_h">'title'</span><span class="br0">]</span><span class="sy0">.</span><span class="st_h">'<br /><br />'</span><span class="sy0">;</span>
<span class="re0">$rand</span> <span class="sy0">=</span> <span class="kw3">array_rand</span><span class="br0">(</span><span class="re0">$item</span><span class="sy0">,</span><span class="nu0">2</span><span class="br0">)</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$item</span><span class="br0">[</span><span class="re0">$rand</span><span class="br0">[</span><span class="st_h">'name'</span><span class="br0">]</span><span class="br0">]</span><span class="sy0">;</span>
<span class="br0">}</span>
 
<span class="sy1">?></span></pre></div></td></tr></table></div>

Comments

  • dan keluar error
    Undefined index: name in D:\wamp\www\random.php on line 29

    Mohon bantuannya
    Thanks before.
  • line 29 mana?
  • line 29 pas penutup<div class='geshi_syntax'><table><tr><td><div class='code'><pre class="php"><span class="sy0">....</span>
    <span class="nu0">26</span><span class="sy0">.</span> <span class="kw1">echo</span> <span class="re0">$item</span><span class="br0">[</span><span class="re0">$rand</span><span class="br0">[</span><span class="st_h">'name'</span><span class="br0">]</span><span class="br0">]</span><span class="sy0">;</span>
    <span class="nu0">27</span><span class="sy0">.</span> <span class="br0">}</span>
    <span class="nu0">28</span><span class="sy0">.</span>
    <span class="nu0">29</span><span class="sy0">.</span> <span class="sy1">?></span></pre></div></td></tr></table></div>
  • Mohon bantuannya para master PHP :(
  • edited February 2011
    key array bertipe numeric, situ kasih opsi return array_rand 2 (artinya antara 0 dan 1), silahkan ente mo pilih yang mana. jadi bukan pake key nama

    misal echo $item[$rand[0]];

    atau

    echo $item[$rand[1]];
  • kasih aja
    ini_set('display_errors',0);

    :d:d
Sign In or Register to comment.