-3

I will re-explain the whole situation as it got confused. And keep in mind i do not know much about coding. I have an array like that:

<?php
$mytestarray = array(
'test1'=>'aaa',
'test2'=>'aaa',
'test3'=>'ccc'
);

Now what i need is to check if the array keys exist in the current url of the webpage. If they exist i will do something if not the rest of the code will go on loading (this code will place in the header of my website). So there are many codes to run if this statement returns false.

So in the array example above http://localhost/test1xxx.php should return True as the url contains test1 which is the first array key http://localhost/test2asdasdasdas.php should also return True.

That is it. I do not need anything more or less or different. As the array values will be used somewhere else, the script should check if array keys exists or not in the url

6
  • 1
    Try array_key_exists() or array_search() Commented Aug 19, 2014 at 18:43
  • possible duplicate of PHP find element key Commented Aug 19, 2014 at 18:44
  • hello sorry but i dont know much about coding. So those do not answer my question as am trying to check if the URL contains this Commented Aug 19, 2014 at 18:46
  • I don't think his problem was to loop/search through the array, but rather than find a string representation or index representation of the current requested URI. Commented Aug 19, 2014 at 18:46
  • just noticed this code works when it is executed on aaa.php however what i need is the left side of the array. so test1. Commented Aug 19, 2014 at 19:55

1 Answer 1

4

EDIT
Improved answer for user.

<?php
$mytestarray = array(
"test1"=>'aaa',
"test2"=>'bbb',
"test3"=>'ccc'
);
// Create a function so it's cooler
function in_URI(array $array)
{
    // Please review the code entirely.
    foreach($array AS $key => $value){
        // If the URI does contain one of the words in the array
        if (strpos($_SERVER['REQUEST_URI'], $key) !== FALSE) {
            // Return that it has been found
            return true;
        }
    }
    // Return that the values have not been found
    return false;
}

if(in_URI($mytestarray)){

    echo "YAAAY! It's in here!";

}else{

    echo "Awwww :(";
}
Sign up to request clarification or add additional context in comments.

9 Comments

thanks but i could not get it work. I need something like. if the current url contains one of the array text then do this. IF does not contain the rest of the code will work
How did you try to implement it? And what do you mean it doesn't work? Did you get any errors?
I am trying to put it into the header of my website. So if someone enters to an url directly which contains a text from my array i will show him an empty page. So this code will load in all of the pages in my website. So the page should go on loading if the person does not get filtered through this code
See the edited answer, and let me know if that was what you're looking for.
i could not get it work. i posted what i did. something is not right
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.