bennyEire Posted July 30, 2013 Share Posted July 30, 2013 Hey peps, I'm a bit of a noob a php so please be gentle, The following code is an associated array which takes info form a search engine such as url,title and snippet, $blekkoArray = array(); $find = array ('http://','https://','www.'); $score = 100; foreach ($js->RESULT as $item) { $blekkoArray[str_replace ($find, '', ($item->{'url'}))] = array( 'title'=> $item->{'url_title'}, 'snippet' => $item->{'snippet'}, 'score' => $score-- ); } when I do a print_r this is the output I get Array ( [example.com] => Array ( [title] => Example title [snippet] => Blah Blah Blah [score] => 100 ) [example2.com] => Array ( [title] => Example title2 [snippet] => Blah Blah Blah2 [score] => 99) [example3.com] => Array ( [title] => Example title3 [snippet] => Blah Blah Blah3 [score] => 98 ) .......) I'm trying to get an output like the following below example.com // I would like this to be a href clickable link Example title Blah Blah Blah example.com 2 Example title Blah Blah Blah 2 and so on, this is the code I've tried foreach ($js->RESULT as $item) { echo "<href={$blekkoArray[str_replace ($find, '', ($item->{'url'}))]}>". $blekkpArray[$key]['title'] . "</a> <br>" . $blekkoArray[str_replace ($find, '', ($item->{'url'}))] . "<br>" . $blekkoArray[$key]['snippet']; echo "<br>"; echo "<br>"; } but when I run the code I'm getting undifined index and undifined variable warnings, can anyone help please Quote Link to comment Share on other sites More sharing options...
Mad programmer Posted July 30, 2013 Share Posted July 30, 2013 You may wish to add a print_r($myArray) between the html pre tags. <pre> </pre>. That will give the desired result! Quote Link to comment Share on other sites More sharing options...
bennyEire Posted July 30, 2013 Author Share Posted July 30, 2013 That will print the array out, but the loop I'm trying returns , Undefined variable: key and Undefined index:, I stumped :-( Quote Link to comment Share on other sites More sharing options...
Mad programmer Posted July 30, 2013 Share Posted July 30, 2013 (edited) For the loop I'd recommend to use a nested foreach.(on the array you've used in your print_r) The first foreach kan be used like: Foreach($array as $key => $value) { ... } Where $key is example.com and value are its children. So then do another foreach inside this one with the $value as an array. That should work! Edited July 30, 2013 by Mad programmer Quote Link to comment Share on other sites More sharing options...
bennyEire Posted July 31, 2013 Author Share Posted July 31, 2013 So would the structure be something like this foreach($bingarray as $key => $value) { foreach($value.....){ // I'm not sure what goes in here echo "<href={$bingArray[str_replace ($find, '', ($item->{'Url'}))]}>". $bingArray[$key]['title'] . "</a> <br>" . $bingArray[str_replace ($find, '', ($item->{'url'}))] . "<br>" . $bingArray[$key]['snippet']; echo "<br>"; echo "<br>"; } } what do I put in the second foreach loop Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted July 31, 2013 Share Posted July 31, 2013 Just out of curiosity - what is the str_replace for? Here is a heavily commented basic example of itterating through a multi-dim' array, maybe it will healp you get the idea of what to do: <?php //$myArray is a pretend array containing keys of userID's and each of these keys relates to a sub array containing keys of firstname, lastname, age, dob and homepage //I will apply only the most basic of html table formating as well as a simple hyperlink for the homepage // //lets get started echo "<table>"; //start the table element foreach($myArray as $topKey=>$subArray){ //This is the ittertation through the top level array, breaking it into key=>value pairs identified by the variables used after the "as" echo "<tr><th>user ID : $topKey </th>"; //create a new table row and echo out the userID [which is the key in the top level of $myArray, and as such is assigned to $topKey by the foreach] into a table header foreach($subArray as $arrayElems){ // This starts itterating through the sub array associated with the key we just echo'd. //notice that as we don't care about accessing the litteral key, only it's associated value, we have assigned only a single variable after the "as" this time. echo "<td>First Name : {$arrayElems['fistname']}</td>"; //echo out the firstname value into the table echo "<td>Last Name : {$arrayElems['lastname']}</td>"; //echo out the lastname value into the table echo "<td>Age : {$arrayElems['age']}</td>"; //echo out the age value into the table echo "<td>Date Of Birth : {$arrayElems['dob']}</td>"; //echo out the dob value into the table echo "<td>Homepage : <a href=\"{$arrayElems['homepage']}\">{$arrayElems['homepage']}</a></td>"; //echo out the homepage value into the table and crate a hyperlink with it too } //close sub level itteraton action block echo "</tr>" //now we are back out to the top level itteration we want to close the table row ready for the next item } //close top level itteration action block echo "</table>"; // at this point all the information has been echoe'd, so it's time to close the table ?> Quote Link to comment Share on other sites More sharing options...
bennyEire Posted July 31, 2013 Author Share Posted July 31, 2013 I'll give this a try, the str_rpl is used to normalize the urls, ie. it takes out http://, www. https:// Quote Link to comment Share on other sites More sharing options...
Solution Muddy_Funster Posted July 31, 2013 Solution Share Posted July 31, 2013 you will need to leave that in in order to have accurate hyperlinks - www. is a subdomain - it is not nesceseraly the same site as http:// which is very different from https:// you can't normalise a dataset when each entry actualy represents a different value Quote Link to comment Share on other sites More sharing options...
bennyEire Posted July 31, 2013 Author Share Posted July 31, 2013 After messing around for like a fill day this code done the trick foreach($jsonObj->d->results as $value){ echo "<a href=\"{$value->Url}\">{$value->Title}</a><p>{$value->Description}</p>". "<br>"; } Thanks for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.