$fetchItem = $itemQuery->fetch(PDO::FETCH_ASSOC);
$fetchLink = $linkQuery->fetch(PDO::FETCH_ASSOC);
This only fetches the first row of each resultset. You need fetchAll
:
$fetchItem = $itemQuery->fetchAll(PDO::FETCH_ASSOC);
$fetchLink = $linkQuery->fetchAll(PDO::FETCH_ASSOC);
and adjust the rest of your code.
foreach($merged as $entry) {
foreach( $entry as $key => $value ) {
echo "${key} => ${value} <br />";
}
}
EDIT:
The call of fetch
only retrieved the first row of the resultset, whereas fetchAll
parses the complete resultset into an Array. So the Objects look like this afterwards:
Array(
[0] => { 'items' => 'Kill Bill' },
[1] => { 'items' => 'Preman' }
)
Array(
[0] => { 'itemLink' => 'Kill Bill' },
[1] => { 'itemLink' => 'Preman' }
)
array_merge
concatenate both arrays to the following:
Array(
[0] => { 'items' => 'Kill Bill' },
[1] => { 'items' => 'Preman' },
[2] => { 'itemLink' => 'Kill Bill' },
[3] => { 'itemLink' => 'Preman' }
)
So we now have a two dimensional array. To traverse the values we need first to select each $entry
, which is done in the outer foreach
and can afterwards access the key/value structure in the inner foreach
.
As pointed out in the other comment: If you want to preserve the connection between items
and itemLink
, you should change the query in the first place to
SELECT items, itemLink FROM menus