简体   繁体   中英

how to loop through associative array and echo out to list?

I have managed to get my first array working but no matter how many code examples I try I cannot step through each array row and echo the three columns / elements out to a

  • .

    The var_dump of my array is:-

    foreach($results as $key=>$value)
        {
        echo $key.": ".$value;
        }
    

    I have tried using this but I did not get the expected results:-

     foreach($results as $key=>$value) { echo $key.": ".$value; } 

    Can someone please help with a code example that loops through the array and echos ID, Title, Questions to a

  • Thanks in advance of your help.

    Jonathan

    Try:

    foreach($results as $k => $v) {
        echo '<li>' . $v['id'] . '</li>';
        echo '<li>' . $v['title'] . '</li>';
        echo '<li>' . $v['questions'] . '</li>';
    }
    

    Or am I missing something?

    The reason you are having problems is because this is a 2D array. You have to iterate twice. try something like this.

    foreach($results as $result)
    {
        foreach($result as $key=>$value)
        {
            echo $key.": ".$value;
        }
    }
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM