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

  • 1
    shouldn't your foreach on the output be over the `$googleArray` instead of the `$all_items` you constructed in the first step – Orangepill Jul 30 '13 at 14:15
  • 3
    You need to learn how to [READ and debug](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) 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. – Anigel Jul 30 '13 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. – Jonathan Römer Jul 30 '13 at 14:20

2 Answers2

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. ;)

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 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 – user2634446 Jul 30 '13 at 15:31
  • Ha yeah, I guess thats the point, I under pressure as it for a college project, thanks for your help – user2634446 Jul 31 '13 at 12:36
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