Using PHP, how do I output/display the values from this array within my web page:
3
-
A table would be appropriate. Convert [digit] to <tr> and [name] to <td>, then surround with <table> tag and clean up.pavium– pavium2010-01-04 04:41:10 +00:00Commented Jan 4, 2010 at 4:41
-
1You could possibly explain a bit better:)? Need the value of the name value. Or any value and not key?eriksv88– eriksv882010-01-04 04:45:39 +00:00Commented Jan 4, 2010 at 4:45
-
I was suggesting that [a single digit] could be replaced by <tr> and [a name or word] could be replaced by <td>. I see several of those in the web page source. A fair bit of cleaning up would be necessary to get a clean <table>, but the array looks like a table to me.pavium– pavium2010-01-04 04:53:51 +00:00Commented Jan 4, 2010 at 4:53
Add a comment
|
3 Answers
1 Comment
DeveloperChris
I don't believe he asked how to output the data like this... He wanted to know how to output the data into a web page. I assumed he used the print_r statement to display the data structure.
Use a foreach statement to iterate over the array echoing each as you go
foreach($data as $k => $v) {
echo "{$k} => {$v}";
}
$data is the input data $k is the key $v is the $value
The above is only useful for a single depth array for an array of arrays like the example data provided you need to use a recursive function then check to see if the value is an array if so call the function recursivally.
If the data structure never changes then some nested loops will do the job. (some people think recursion is evil, I pity them)
DC
Comments
Start with our Array
$myarray = array(
"Key1" => "This is the value for Key1",
"Key2" => "And this is the value for Key2"
);
Show All Output (Helpful for Debugging)
print_r($myarray);
/* or */
var_dump($myarray);
Show only a Single Value
print $myarray["Key1"]; // Prints 'This is the value for Key1'
print $myarray[0]; // Prints 'This is the value for Key1'