Error finding specific word in array

I need to check if in my array is the word “Location”.

I have tried those.

With array_search

echo array_search('Location', $the_array);

echo array_search("Location", $the_array);

echo array_search('/Location/', $the_array);

echo array_search("/Location/", $the_array);

None of them gives me the key (in my case the key should be 2).


With in_array

if (in_array('Location', $the_array)) {
    echo "Yes";
} else {
    echo "No";
}
if (in_array('/Location/', $the_array)) {
    echo "Yes";
} else {
    echo "No";
}
if (in_array("Location", $the_array)) {
    echo "Yes";
} else {
    echo "No";
}
if (in_array("/Location/", $the_array)) {
    echo "Yes";
} else {
    echo "No";
}

In every case i get the No response


The array

Array
(
    [0] => Tynie 
    [1] =>   
    [2] =>        Tynecastle Stadium       Location in Edinburgh 
    [3] => Gorgie, Edinburgh, Scotland
    [4] => 55°56′21″N  3°13′56″W    /  55.93917°N 3.23222°W  / 55.93917; -3.23222     
)

those will look for a full match (i.e. the complete value must match the given string). but you only have a partial match. not sure why you would expect those to work since the use cases are clearly described and shown in the Manual.

I guess trying random variations like singe ↔ double quotes, with ↔ without slashes, and different functions etc. without understanding what is happening is part of the learning process. I confess I did it when I started and some rare times doing so “worked” or at least seemed to.

But I long ago realized that sifting through the documentation and looking at the examples yielded much better use of my time in getting to a solution than testing guesses.

As Dormilich posted, those functions deal with exact array values as a whole, not value components.

I think you will need to use a callback that uses string functions inside it to test for the presence of the needle.

Maybe array_walk could work for what you need?

http://php.net/manual/en/function.array-walk.php

1 Like

better use array_filter() : http://php.net/array-filter

1 Like

Thanks to all for replays , can you tell me how to achieve what i need, to find the word “Location” in the array and to give back the key ?

Which callback are you using?

AFAIK they should either already take the key as a parameter or have a Flag that lets you send it as one.

As far as finding the string, I doesn’t look like any regex would be needed so a string function should be sufficient. Though you may want “mb” if you are dealing with characters outside of the 0-127 range.
* pay note to documentation when it says to use the “===” operator,

strpos() - Find the position of the first occurrence of a substring in a string
stripos() - Find the position of the first occurrence of a case-insensitive substring in a string
strrpos() - Find the position of the last occurrence of a substring in a string
strripos() - Find the position of the last occurrence of a case-insensitive substring in a string
strstr() - Find the first occurrence of a string

http://php.net/manual/en/ref.strings.php

$foundKey = false;
$search = 'Location';

foreach($array as $key=>$val){
    if (strstr($val, $search)) {
        $foundKey = $key;
        break;
    }
}

if ($foundKey === false) {
    // nothing found
} else {
    // see $foundKey for result
}

You could use preg_grep() to match a pattern against the array items. This function returns an array with the matching items.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.