Using PHP, how do I output/display the values from this array within my web page:
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.
DeveloperChris
2010-01-04 04:55:45
+1
A:
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
DeveloperChris
2010-01-04 04:39:38
A:
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'
Jonathan Sampson
2010-01-04 22:31:44