tags:

views:

104

answers:

2

Hi

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:-

    array(27) { [3]=>  array(3) { ["id"]=>  string(3) "295" ["title"]=>  string(24) "ask.sqlservercentral.com" ["questions"]=>  int(57) } [4]=>  array(3) { ["id"]=>  string(3) "287" ["title"]=>  string(36) "LensFail.com - Photography Questions" ["questions"]=>  int(42) } [2]=>  array(3) { ["id"]=>  string(3) "437" ["title"]=>  string(12) "VideoWTF.com" ["questions"]=>  int(37) } [13]=>  array(3) { ["id"]=>  string(2) "92" ["title"]=>  string(8) "Moms4Mom" ["questions"]=>  int(36) } [9]=>
    

    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

    +4  A: 

    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?

    treznik
    thank thank you! Apologies I am as a dumb as a donkey and only learning - I get it now!!!
    Jonathan Lyon
    +1  A: 

    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;
        }
    }
    
    Bart