-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

Karl Heinz
  • 13
  • 3
  • 1
    Try [`array_key_exists()`](http://php.net/manual/en/function.array-key-exists.php) or [`array_search()`](http://php.net/manual/en/function.array-search.php) – Funk Forty Niner Aug 19 '14 at 18:43
  • possible duplicate of [PHP find element key](http://stackoverflow.com/questions/1638527/php-find-element-key) – Shawn Aug 19 '14 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 – Karl Heinz Aug 19 '14 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. – Zander Rootman Aug 19 '14 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. – Karl Heinz Aug 19 '14 at 19:55
  • I'd recommend this for a read: http://stackoverflow.com/questions/176264/whats-the-difference-between-a-uri-and-a-url :) – Zander Rootman Aug 19 '14 at 21:06

1 Answers1

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 :(";
}
Zander Rootman
  • 2,178
  • 2
  • 17
  • 29
  • 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 – Karl Heinz Aug 19 '14 at 18:53
  • How did you try to implement it? And what do you mean it doesn't work? Did you get any errors? – Zander Rootman Aug 19 '14 at 18:54
  • 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 – Karl Heinz Aug 19 '14 at 18:58
  • See the edited answer, and let me know if that was what you're looking for. – Zander Rootman Aug 19 '14 at 19:01
  • i could not get it work. i posted what i did. something is not right – Karl Heinz Aug 19 '14 at 19:33
  • Sorry took so long to reply. Give me an example of a a URL, where one of the key words will be found. I tested the code and it worked fine by me for the following URL: `http://localhost/test/test.php?aaa` – Zander Rootman Aug 19 '14 at 20:42
  • hey np. first of all i need the opposite of the code. you just edited the comment on the code. I want "if url contains do this". Also this code only works for the first line of the array. it does not check the second line. also i want it to check the array key not the value itself. – Karl Heinz Aug 19 '14 at 20:50
  • Yeah there's a tiny bug lol.. It's late where I come from :P – Zander Rootman Aug 19 '14 at 20:55
  • I re-edited the question again to explain what i need in detail.thanks – Karl Heinz Aug 19 '14 at 21:01