在下面的数组中,您会看到userId = 16
。但是,当我运行foreach
循环时,它会返回userId = 1
。为什么它会返回1
而不是16
?
Array
(
[userId] => 16
[positionTitle] => Array
(
[0] => j1
[1] => j2
)
[company] => Array
(
[0] => c1
[1] => c2
)
[jobDescription] => Array
(
[0] => d1
[1] => d2
)
[startDate] => Array
(
[0] => 03/01/2013
[1] => 03/03/2013
)
[endDate] => Array
(
[0] => 03/02/2013
[1] => 03/04/2013
)
[jobCity] => Array
(
[0] => city1
[1] => city2
)
[jobState] => Array
(
[0] => state1
[1] => state2
)
)
循环代码:
foreach($updates as $key => $value){
echo $key . " " . $value[0] . "<br/>";
}
示例输出:
userId 1
positionTitle j1
company c1
jobDescription d1
startDate 03/01/2013
endDate 03/02/2013
jobCity city1
jobState state1
答案 0 :(得分:6)
你的问题在这里:
foreach($updates as $key => $value){
echo $key . " " . $value[0] . "<br/>";
}
输出字符串“16”的第1个字符(第0个元素)。
将您的代码更改为此内容以获得所需的效果:
foreach($updates as $key => $value){
if(!is_array($value))
echo $key . " " . $value . "<br/>";
else
echo $key . " " . $value[0] . "<br/>";
}