extracting information from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ubunken
Forum Newbie
Posts: 2
Joined: Tue Jan 06, 2009 1:14 am

extracting information from array

Post 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. :)
Last edited by ubunken on Tue Jan 06, 2009 3:58 am, edited 1 time in total.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: extracting information from array

Post by it2051229 »

try making use of substr (substring) function but you still have to do it in a loop.
ubunken
Forum Newbie
Posts: 2
Joined: Tue Jan 06, 2009 1:14 am

Re: extracting information from array

Post 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
Last edited by ubunken on Tue Jan 06, 2009 4:08 am, edited 1 time in total.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: extracting information from array

Post by it2051229 »

wait i'm sorry it shouldn't be substr.... try checking this array function
http://www.php.net/function.in-array
Post Reply