Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

html - PHP: echoing array only returns the word array instead of its content

I am very new to PHP and hope you can help me with this.

I am fetching some data from SQL and try to create an array that I want to echo on a page.

When I echo the result from the following ($output) then this only returns the word "array" insteaed of the items that I am trying to add to it.

Can someone tell me what I am doing wrong here and provide me a short explanation as well ?

$c = "";
$i = 0;
$arr = array();
$output = ''
foreach ($objNames->names as $names) {
    $c = "<img src='images/photos/photo_" . str_replace(" ", "_", $names->member) . ".png' alt='' class='clickable flagLink trackHC' />&nbsp;" . $names->member . " &nbsp;&nbsp;";
    array_push($arr, $c);
    $i++;
}
if($i != 0) {
    $output = $arr;
}


<div id="output"><?php echo $output; ?></div>

Many thanks for any help, Mike.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cant just echo arrays.

You have to loop through it with a foreach for example:

$c = "";
$i = 0;
$arr = array();
$output = ''
foreach ($objNames->names as $names) {
    $c = "<img src='images/photos/photo_" . str_replace(" ", "_", $names->member) . ".png' alt='' class='clickable flagLink trackHC' />&nbsp;" . $names->member . " &nbsp;&nbsp;";
    array_push($arr, $c);
    $i++;
}
if($i != 0) {
    $output = $arr;
}

foreach($output as $row) {
?>
    <div id="output"><?php echo $row; ?></div>
<?php
}

This should work for you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...