I just built a simple foreach loop to run through an array, but nothing is displaying. No php errors by the way.
Can someone tell me why this isn't working?
$test = array (
"1" => array(
"name"=>"something"
),
"2" => array(
"name"=>"something"
)
);
foreach ($test as $key => $arr) {
echo $arr[$key]["name"];
}
只需使用$arr["name"]
而不是$arr[$key]["name"]
。
I think you meant...
foreach ($test as $key => $arr) {
echo $test[$key]["name"];
}
Or, even more simply...
foreach ($test as $key => $arr) {
echo $arr["name"];
}
foreach ($test as $key => $arr) {
echo $test[$key]["name"];
}
OR
foreach ($test as $key => $arr) {
echo $arr["name"];
}
Your array is written in a way that "1" and "2" are values and not keys.
what you need is:
$test = array (
array(
"name"=>"something"
),
array(
"name"=>"something"
)
);
also, you have a typo on your foreach. you need $test[$key]
and not $arr[$key]
You should use the $key key in the array reference.
foreach ($test as $arr) {
echo $arr["name"];
}
You can address the field of the array like
foreach ($test as $key=>$arr) {
$test[$key][$name]
}
but doing so you do not use the direct reference to the inner arrays
Try this,
foreach ($test as $key => $arr) {
echo $arr["name"];
}
Use
echo $arr["name"];
or
echo $test[$key]["name"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.