Page 1 of 1

extracting information from array

Posted: Tue Jan 06, 2009 3:45 am
by ubunken
Hi all I was wondering if it is possible to extract the entire key of an array and storing it in a variable.

My array contains strings:

$arr[0] = I am a boy
$arr[1] = I am a girl
$arr[2] = I am a mom

Is it possible to search through the entire array for the element by just typing in a keyword for example "boy"?

my code currently goes like this:

$search = array_search("boy", $arr);
$who = explode(" ", $search);
$captured = $who[3];


Hope you guys can help me with it because it does not work currently. :)

Re: extracting information from array

Posted: Tue Jan 06, 2009 3:56 am
by it2051229
try making use of substr (substring) function but you still have to do it in a loop.

Re: extracting information from array

Posted: Tue Jan 06, 2009 4:02 am
by ubunken
it2051229 wrote:try making use of substr (substring) function but you still have to do it in a loop.
Is there anyway to go about doing it? Because i have to echo the $captured. Currently the array i have now was obtained by exploding output that was parsed into variables from command line. And in actual fact i do not have any idea what information will be displayed thus i can't explicitly state the information that i want to echo. The only information i do know is that I need the third variable as shown in my sample code

Re: extracting information from array

Posted: Tue Jan 06, 2009 4:06 am
by it2051229
wait i'm sorry it shouldn't be substr.... try checking this array function
http://www.php.net/function.in-array