treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Iterating through an associative array from SQL query

  • Hi guys,

    I have an associative array (arrInfo) that I can't seem to get the data from. Here's the print_r output as an example:

    Array ( [0] => Array ( [label_text] => Area of coverage [data] => 160 m� ) [1] => Array ( [label_text] => Dimensions [data] => 352 x 277 x 112 (mm) ) [2] => Array ( [label_text] => Finish [data] => Aluminium ) [3] => Array ( [label_text] => Lamp [data] => EcoLite energy saving 13 watt (�2) ) [4] => Array ( [label_text] => Lead length [data] => 1.1m ) [5] => Array ( [label_text] => Mounting [data] => Wall mount, ceiling suspend or free stand ) [6] => Array ( [label_text] => Power input [data] => 230Vac 50Hz (standard UK mains supply) ) [7] => Array ( [label_text] => Warranty period [data] => 1 year ) )

    I need to iterate this array to create a HTML definition list (DL) with the DD term as the [label_text] and the DT as the [data value] for each row.

    The code I have right now is:

    foreach($arrInfo as $row) {
    echo ''.$row['label_text'].''.$row['data'].'';
    }

    but it only returns the last row of the array (arrInfo)?
  • OK. Scratch that - it was working fine. I was building an output string instead of echo'ing and discovered that I was overwriting the string with each loop! DUH. :)
  • lol, I was going to tell you that but you figured it out! Good work ;)