Jump to content

Trying to print out an array in html


bennyEire
Go to solution Solved by Muddy_Funster,

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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 by Mad programmer
Link to comment
Share on other sites

 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

Link to comment
Share on other sites

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
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.