0

The following code is an associated array which takes info form a search engine such as url,title and snippet,

$googleArray = array(); 

    $find = array ('http://','https://','www.');                    
    $score = 100;                  



foreach ($all_items as $item)  // 
{   
    $googleArray[str_replace ($find, '', ($item->{'link'}))] = array(         
    'title'=> $item->{'title'},
    'snippet' => $item->{'snippet'},
    'score' => $score--
     );

}

I know want to print this out in html on a webpage, I've tried this code.

foreach ($all_items as $item)
{   
    echo "<href={$googleArray[($item->{'link'})]}>". $googleArray['title'] . "</a> <br>" .        
    $googleArray['link'] . "<br>" . $googleArray['snippet'];
    echo "<br>"; echo "<br>";

} 

These are the errors I'm getting

Notice: Undefined index: http://www.time.com/ 

Notice: Undefined index: title 

Notice: Undefined index: link 

Notice: Undefined index: snippet 

Can anyone see where I'm going wrong

3
  • 1
    shouldn't your foreach on the output be over the $googleArray instead of the $all_items you constructed in the first step Commented Jul 30, 2013 at 14:15
  • 3
    You need to learn how to READ and debug error messages. Everything you needed to solve the problem was in the error message. $googleArray does not contain the keys ['title'] ['link'] etc, print_r or var_dump it and see that it contains what you think it does. Commented Jul 30, 2013 at 14:16
  • Stop copy/pasting code which you dont understand. The error says it all, if you dont understand i suggest go back to the basics and check wat undefined means. Commented Jul 30, 2013 at 14:20

2 Answers 2

1

To read from the array, you are using the link property of an item as a key:

echo "<href={$googleArray[($item->{'link'})]}>

But earlier in the code, you are not using that exact link to write the value. You write using

$googleArray[str_replace ($find, '', ($item->{'link'}))];

so you are not using $item->{'link'}, but a modified version (with str_replace) of that link.

So that is one issue.

The other issue, is that title and snippet are keys in an array that is a value of a key in $googleArray, so to get those values, you should read $googleArray[$key]['snippet'] instead of $googleArray['snippet']. $key in this case being the corrected key after you fixed the first problem. ;)

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

2 Comments

I've done that and I see what you mean thanks, now I'm getting undifined index and undifined variable, I'v done a var dump and snippet,title and $googleArray[str_replace ($find, '', ($item->{'link'}))] are in the array, I wonder do I need to change the foreach loop, I have changed it output $googleArray
Ha yeah, I guess thats the point, I under pressure as it for a college project, thanks for your help
0

It was as simple has this in the end

foreach($all_items as $item){
        echo  "<a href=\"{$item->link}\">{$item->title}</a><p>{$item->snippet}</p>". "<br>";

    }

and it only took me a full day to figure it out, lol

Comments

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.