Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parsing Associative Array

I have a quick question that I just can't seem to figure out even though it should be straightforward.

I have an associative array of the following structure:

 [uk] => Array
        (
            [TW] => 1588
            [LY] => 12936
            [LW] => 13643
        )

I am displaying it in an HTML table as follows.

foreach ($leads as $country) {
    echo '<tr><td>' . $country . '</td><td>' . $country['TW'] . '</td><td>' . $country['LY'] . '</td><td>' . $country['LW'] . '</td></tr>';
}

but the country comes out as Array so I'm just wondering what I am doing wrong to get the uk part out.

Output

Array   1588    12936   13643
like image 327
martincarlin87 Avatar asked Dec 20 '22 15:12

martincarlin87


1 Answers

Use something like:

foreach ($leads as $name => $country) {
    echo '<tr><td>' . $name. '</td><td>' . $country['TW'] . '</td><td>' . $country['LY'] . '</td><td>' . $country['LW'] . '</td></tr>';
}

Now, $name in the loop is the key (in this case 'uk') and $country is the value of the element, in this case the array (TW => 1588, LY => 12936, LW => 13643)

like image 106
rael_kid Avatar answered Dec 25 '22 23:12

rael_kid