Sunday, October 12, 2008

How to find the array key for a given value using php - array_search()

You can use array_search() for searching a value within a given array.
You can search a given value in an array and return the relevant key using array_search()


<?php
$array = array(0 => 'Tom', 1 => 'Kusal', 2 => 'Jerry', 3 => 'Jhonny');

$key = array_search('Tom', $array);
echo $key; // $key = 0;

/* sample
$key = array_search('Kusal', $array); // $key = 1;
$key = array_search('Jerry', $array); // $key = 2;
$key = array_search('Jhonny', $array); // $key = 3;
sample */


?>


Get Free Sinhala IT Learning Videos Kuppiya.com

Subscribe To My Blog

No comments:

Post a Comment