我正在尝试遍历此数组,来自数据库并将Field_Name
和Field_Value
输出到网页。
Array
(
[0] => stdClass Object
(
[Field_Name] => First Name
[Field_Value] => søren
)
[1] => stdClass Object
(
[Field_Name] => Last Name
[Field_Value] => hansen
)
[2] => stdClass Object
(
[Field_Name] => Email
[Field_Value] => s.hansen@hansens.dk
)
[3] => stdClass Object
(
[Field_Name] => medlem
[Field_Value] => ja
)
[4] => stdClass Object
(
[Field_Name] => Profilbillede
[Field_Value] => sCZ
)
[5] => stdClass Object
(
[Field_Name] => bestyrelse
[Field_Value] => nej
)
)
如果我使用:
$count=0;
while ($count < 6) {
$newdata = $data[$count];
foreach ($newdata as $key => $val) {
echo "<div class='list'>" . $key . " = " . $val . "</div><br><br>";
}
$count++;
}
我明白了:
Field_Name = First Name
Field_Value = søren
Field_Name = Last Name
Field_Value = hansen
依旧......
但我需要摆脱Field_Name
和Field_Value
,以便数组看起来像:
First Name: Søren
Last Name: Hansen
我尝试使用$data[0]["Field_name"]
,但它不起作用。我现在疯了,所以我需要朝着正确的方向努力。
我希望我已经说清楚我需要做什么。
答案 0 :(得分:0)
辅助尺寸不是数组,它们是对象!所以你不能那样访问它们。
简而言之,您应该可以执行类似$ data [0] - &gt; Field_name的操作 或者将它们转换为数组然后$ data [0] [“Field_name”],会起作用!
答案 1 :(得分:0)
我认为你正在寻找类似......
的东西//assuming you have data stored in $data..
$newdata = array();
foreach ($data as $object){
if ($object->Field_Name && $object->Field_Value){
$newData[$object->Field_Name] = $object->Field_Value;
}
}
答案 2 :(得分:0)
为此更改你的foreach:
$count=0;
while ($count < 6) {
$newdata = $data[$count];
echo "<div class='list'>" . $newdata["Field_Name"] . " = " . $newdata["Field_Value"] . "</div> <br><br>";
$count++;
}