0

I am having a bit of trouble with some code where in an array I have a list of 2 and the function is only displaying the last in the list.

Here is the code:

<?php


  function getKeywordPosition($theurl,$thekeywords) {

    $theurl = $theurl;
    $thekeywords = $thekeywords;

    $found = false;
    $x = 0;

    for($x; $x < 64 && $found == false;)
{
    $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
    . "q=".stripslashes(str_replace(' ', '%20', $thekeywords)).'&start='.$x;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.boo.com');
    $body = curl_exec($ch);
    curl_close($ch);

    $json = json_decode($body);

    $x4 = $x + 4;
    $old_x = $x;

    for($x; $x < $x4 && $found == false; $x = $x + 1)
        {

            if (strpos($json->responseData->results[$x-$old_x]->unescapedUrl, strtolower($theurl)) !== false) 
            {
                $found = true;
            }

        }

        // now have some fun with the results...
    }

    if($found)
    {
        echo '<strong>'.$theurl.'</strong> is located as the <strong>'.$x.'</strong> result when searching for <strong>'.stripslashes($thekeywords).'</strong>';
        echo '<br>';
    }

  }


    $list = array('php.com'=>'php', 'php.com'=>'php');

    foreach($list as $key => $value){

        getKeywordPosition($key,$value);

    }



?>

Why is this not working properly?

2
  • 2
    Have you tried printing out the value of $x as it's iterating through? My guess is that it's your nested loop that's bugging it up. Commented Apr 7, 2014 at 11:31
  • What is this meant to express: $list = array('php.com'=>'php', 'php.com'=>'php');? The two keys are identical, so you end up with only a single element. Certainly a foreach loop based on that array will only give single iteration... Commented Apr 7, 2014 at 11:33

1 Answer 1

2

Unless this is a badly contrived example, then th issue is you have duplicate keys in your array:

$list = array('php.com'=>'php', 'php.com'=>'php');

This array has a single entry

You coud refactor like so:

$list = array(

    array('url'=>'php.net', 'keyword'=>'php'),
    array('url'=>'php.net', 'keyword'=>'arrays'),
    array('url'=>'php.net', 'keyword'=>'anotherkeyword')
    );


foreach($list as $entry){

    getKeywordPosition($entry['url'], $entry['keyword']);

}
Sign up to request clarification or add additional context in comments.

7 Comments

If I was to do this: $list = array('php.com'=>'php', 'php.com'=>'php code'); ...it will just display the last one ... keywords are different here and if I did this: $list = array('php.net'=>'php', 'php.com'=>'php code'); which are totally different, I still get the last result only, so it has to be something else
Are you sure? So if you remove the foreach loop and add two function calls: getKeywordPosition('php.net','php'); getKeywordPosition('php.com','php code'); then do you only get one or both results?
If I do this: getKeywordPosition('php.net','php'); getKeywordPosition('php.net','php.net'); I get 2 results
and if I use this: $list = array('php.net'=>'php', 'php.net'=>'php.net'); foreach($list as $key => $value){ getKeywordPosition($key,$value); } I just get the last one
Right, thats consistent with my point about array indexes. They must always be unique, so the example in your last comment will fail. so $list = array('php.net'=>'php', 'cat.com'=>'cat'); should get two results as well
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.