Reputation: 3
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
Upvotes: 0
Views: 91
Reputation: 3
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
Upvotes: 0
Reputation: 116110
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. ;)
Upvotes: 1