mencari array berdasar keys dan mengembalikan indeks array


untuk mendapatkan indeks array dari hasil pencarian key,kita dapat menggunakan array_search menggunakan php berikut caranya

misalkan kita punya array asosiatif (arrary dengan key custom) binatang


<code><span class="pln">$binatang </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">(</span></code>

<code> <span class="str">'no1'</span> <span class="pun">=></span> <span class="str">'gajah'</span><span class="pun">,</span></code>

<code> <span class="str">'no4'</span> <span class="pun">=></span> <span class="str">'burung'</span><span class="pun">,</span> </code>

<code> <span class="str">'hai'</span> <span class="pun">=></span> <span class="str">'naga'</span><span class="pun">,</span> </code>

<code> <span class="str">'you'</span> <span class="pun">=></span> <span class="str">'nyamuk'</span><span class="pun">,</span> </code>

<code> <span class="str">'1'</span> <span class="pun">=></span> <span class="str">'kecoa'</span><span class="pun">,</span> </code>

<code> <span class="str">'34'</span> <span class="pun">=></span> <span class="str">'hamster'</span><span class="pun">,</span> <span class="pun">);</span></code>

<code></code>// misalnya kita mau cari indeks sebiah arrary dengan key  'you' caranya cari dulu seua key dalam array tersebut

$keys = array_keys($binatang);

//kemudian cari indeksnya dari key pencarian key 'you'

$index = array_search('you',$keys);

<code><span class="pun">

semoga berguna 🙂

referensi:

Iklan

tinggalkan pesan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: