Sergi Khizanishvili
Sergi Khizanishvili

Reputation: 587

foreach loop is giving incorrect result

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

Answers (1)

pr1nc3
pr1nc3

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

Related Questions