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

后端 未结 3 1233
傲寒
傲寒 2020-12-12 08:07

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

相关标签:
3条回答
  • 2020-12-12 08:16

    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!

    0 讨论(0)
  • 2020-12-12 08:25

    If you wish to just see/print the contents of your array, try this:

    print_r ($output);
    
    0 讨论(0)
  • 2020-12-12 08:31

    An array must be printed using:

    print_r($arrayValue)
    

    you can keep the print value also (EDITED):

    $x = print_r($arrayValue, true)
    

    but I prefer the json way if it's for logging purposes:

    echo json_encode($arrayValue)
    

    keeping in mind every value must be json-serializable.

    ...

    Doing a plain echo $arrayValue will always print "Array" word without any content. Yes, it's a bit unintuitive (since other languages like python don't behave like that) but it is what it is.

    0 讨论(0)
提交回复
热议问题