Reputation: 587
I'm trying to use foreach loop for an array. When I print_r the array it gives me following:
Array ( [ID] => 1 [Value] => NYC125 [User] => 1 [Uses] => 1 [Amount] => 10 [End] => 1574467200 )
But when I'm trying to use foreach it gives me this kind of strange thing:
1
1
N
N
N
1
1
1
1
1
1
1
1
1
1
1
1
My foreach code is:
foreach ($codes as $code) {
echo
$code['ID'] . '<br/>' .
$code['Value'] . '<br/>' .
$code['User'] . '<br/>';
}
Could you help and give me a hint what I'm doing wrong?
Upvotes: 0
Views: 50
Reputation: 8338
Your array values are not nested so no reason to loop around them. Just access them straight .
$codes = [
'ID' => 1,
'Value' => 'NYC125',
'User' => 1,
'Uses' => 1,
'Amount' => 10,
'End' => 1574467200
];
//Simple echo them
echo
$codes['ID'] . '<br/>' .
$codes['Value'] . '<br/>' .
$codes['User'] . '<br/>';
Output:
1
NYC125
1
Upvotes: 2