Reputation: 1244
I have an array as follows in $newarray
Array
(
[111] => Array
(
[123] => 0
[124] => 0
[125] => 0
[126] => 0
[127] => 0
[128] => 0
[129] => 0
[130] => 1
[131] => 1
[132] => 1
[133] => 1
[134] => 1
[135] => 1
)
[222] => Array
(
[123] => 0
[124] => 0
[125] => 0
[126] => 1
[127] => 1
[128] => 1
[129] => 1
[130] => 1
[131] => 1
[132] => 1
[133] => 1
[134] => 1
[135] => 1
)
[333] => Array
(
[256] => 0
[321] => 0
[456] => 0
[489] => 0
[652] => 1
[741] => 1
[965] => 0
)
)
I need to grab the key names (111, 222 and 333 in this case) in a foreach:
The following always spits out "222" 3 times, regardless of what I do. Anything obvious?
foreach($newarray as $value) {
echo key($newarray) . "<br />";
}
Output
222
222
222
Upvotes: 0
Views: 138
Reputation: 3849
This is the correct way to use foreach
foreach($newarray as $key=>$value) {
echo $key . "<br />";
}
or you can use
print_r(array_keys($newarray));
Upvotes: 4
Reputation: 1224
try this:
foreach($newarray as $key => $value)
{
echo $key . "<br />";
}
Upvotes: 4