Jump to content

Recommended Posts

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 :)

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
}
?>

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);

 

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";
}
?>

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

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?

$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");

  • 3 weeks later...

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.