ubunken Posted January 6, 2009 Share Posted January 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/ Share on other sites More sharing options...
gevans Posted January 6, 2009 Share Posted January 6, 2009 If you just want the key of the array field that meets the criteria you could try this function; (not tested, just written) <?php /* $arr is the array to search $needle is the search term */ function checkArray($arr, $needle) { foreach($arr as $key => $value){ $found = strrpos($value, $needle); if ($found !== false) { return $key } } return FALSE; } ?> Please note that this can return the key 0 so when checking your return do the following; <?php $result = checkArray($yourArray, $yourNeedle); if($result === FALSE){ //not found } else { //found } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-730756 Share on other sites More sharing options...
RussellReal Posted January 6, 2009 Share Posted January 6, 2009 array_search already returns the key.. $arr[0] = 'I am a boy'; $arr[1] = 'I am a girl'; $arr[2] = 'I am a mom'; $search = array_search("boy", $arr); $who = explode(" ", $arr[$search]); print_r($who); Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-730883 Share on other sites More sharing options...
flyhoney Posted January 6, 2009 Share Posted January 6, 2009 Yeah, array_search does exactly what you want: <?php $arr[0] = 'I am a boy'; $arr[1] = 'I am a girl'; $arr[2] = 'I am a mom'; $key = array_search("boy", $arr); echo $arr[$key]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-730897 Share on other sites More sharing options...
flyhoney Posted January 6, 2009 Share Posted January 6, 2009 More robust code: <?php $query = 'boy'; $arr = array( 'I am a boy', 'I am a girl', 'I am a mom' ); if (($key = array_search($query, $arr)) !== false) { echo $arr[$key]; } else { echo "$query was not found in array"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-730900 Share on other sites More sharing options...
ubunken Posted January 7, 2009 Author Share Posted January 7, 2009 Hi guys thanks for the help. However, it seems to have a minor issue. If i change the query to for example 'mom' , the array_search seems like it can only transverse the first key. Even if i change the $query = 'mom'; the result printed out will still be 'boy'; after testing it seems like it is unable to search for the keyword 'mom' from the entire string. I tried changing the search to 'i am a mom' and it prints out perfectly, however this will not work in my actual scenario because 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 thing i do know is that I need the third variable Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731252 Share on other sites More sharing options...
ubunken Posted January 7, 2009 Author Share Posted January 7, 2009 Is there any way for me to just look for part of the string? And entering maybe some sort of wild card in front of the keyword? Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731257 Share on other sites More sharing options...
.josh Posted January 7, 2009 Share Posted January 7, 2009 You have to loop through each element of the array and do a substring search. Which is pretty much what gevan's suggestion does. Though, I think I'd probably use stripos instead. Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731263 Share on other sites More sharing options...
ubunken Posted January 7, 2009 Author Share Posted January 7, 2009 Hi i tried using the way suggested however i received an error that states "Parse error: syntax error, unexpected T_STRING, expecting'("in C:\xampp\htdocs\test.php on line 14" below is my code: <?php $clamarr = array( 'i am high', 'i am low', 'i am found' ); function checkArray($arr, $search) { for each($arr as $key => $value) { $find = strpos($value, $search); if ($find !== false) { return $key; } } return FALSE; } $result = checkArray($clamarr, $found); if ($result !== FALSE) { echo $result; }else{ echo "not found"; } ?> Does anyone know what's wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731278 Share on other sites More sharing options...
DarkWater Posted January 7, 2009 Share Posted January 7, 2009 You wrote: for each Take out the space. Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731301 Share on other sites More sharing options...
ubunken Posted January 7, 2009 Author Share Posted January 7, 2009 Thank you DarkWater, the method works now, however it is still unable to print out the variable. Seems like there's an error somewhere. It looks like it is not going through each element in the array Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731308 Share on other sites More sharing options...
.josh Posted January 7, 2009 Share Posted January 7, 2009 $result = checkArray($clamarr, $found); You aren't assigning anything to $found. $found should be the word you are looking for. Either do $found = "enterwordhere"; $result = checkArray($clamarr, $found); or $result = checkArray($clamarr, "enterwordhere"); Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731316 Share on other sites More sharing options...
ubunken Posted January 7, 2009 Author Share Posted January 7, 2009 Thank you guys for the help Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-731424 Share on other sites More sharing options...
ubunken Posted January 29, 2009 Author Share Posted January 29, 2009 Hi guys i was wondering if there was a way for me to have a case insensitive search for foreach(). For example <?php $clamarr = array( 'i am high', 'i am low', 'i am Found' ); function checkArray($arr, $search) { for each($arr as $key => $value) { $find = strpos($value, $search); if ($find !== false) { return $key; } } return FALSE; } $result = checkArray($clamarr, found); if ($result !== FALSE) { echo $result; }else{ echo "not found"; } ?> The main purpose of this is that i would be using this method for several arrays and in each array the case of the word found would be different (i.e. Found, found, FOUND) so i was thinking if there was a way for me to traverse the arrays with the method instead of manually substituting the value "found" for all the different arrays. Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-749178 Share on other sites More sharing options...
ubunken Posted January 29, 2009 Author Share Posted January 29, 2009 realised it should have been "stripos". Thanks Quote Link to comment https://forums.phpfreaks.com/topic/139658-solved-extracting-information-from-array/#findComment-749200 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.