PHP可以';t在不使用print\r的情况下打印数组元素
我正在使用一个db查询,它接收一个州和一个城市,然后输出10个字段。目前,我只能使用print\r查看这些字段。我尝试了一个关于a for循环的建议来打印字段,但是我无法让它正常工作。代码如下:PHP可以';t在不使用print\r的情况下打印数组元素,php,arrays,Php,Arrays,我正在使用一个db查询,它接收一个州和一个城市,然后输出10个字段。目前,我只能使用print\r查看这些字段。我尝试了一个关于a for循环的建议来打印字段,但是我无法让它正常工作。代码如下: if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } if (mysql_num_rows($result) > 0) { while ($
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row)."</p>";
$arrayLength = count($row);
for ($i = 0; $i < $arrayLength; $i++){
echo "arrayName at[" . $i . "] is: [" .$row[$i] . "]<br>\n";
}
}
}
知道我为什么不能正确打印字段及其值吗?此外,如果查询返回多行,则我的代码将失败,因为当前代码实际上无法容纳它
我将$I放入for循环的主体中,以查看它是否正常工作。理想情况下,我会在$I所在的位置设置字段名,并在冒号后面设置字段右边的值。您的数组键是
'id'
,'zip\u code'
等。数组的0
,1
等索引中没有任何内容
foreach ($row as $key => $value) {
echo "arrayName at[" . $key . "] is: [" . $value . "]<br>\n";
// which is the same as:
echo "arrayName at[" . $key . "] is: [" . $row[$key] . "]<br>\n";
}
foreach($key=>$value的行){
echo“位于[“$key.”的arrayName是:[“$value.”]
\n;
//这与:
echo“位于[“$key.”的arrayName是:[“$row[$key]。”]
\n”;
}
您的数组键是'id'
,'zip\u code'
等。数组的0
,1
等索引中没有任何内容
foreach ($row as $key => $value) {
echo "arrayName at[" . $key . "] is: [" . $value . "]<br>\n";
// which is the same as:
echo "arrayName at[" . $key . "] is: [" . $row[$key] . "]<br>\n";
}
foreach($key=>$value的行){
echo“位于[“$key.”的arrayName是:[“$value.”]
\n;
//这与:
echo“位于[“$key.”的arrayName是:[“$row[$key]。”]
\n”;
}
是,因为它返回一个关联数组
这意味着您必须像这样访问元素:
例如,$row[“id”]
你想要的是这个
foreach($row as $key => $value)
echo "arrayName at[" . $key . "] is: [" .$value . "]<br>\n";
foreach($key=>$value的行)
echo“位于[“$key.”的arrayName是:[“$value.”]
\n;
是,因为它返回一个关联数组
这意味着您必须像这样访问元素:
例如,$row[“id”]
你想要的是这个
foreach($row as $key => $value)
echo "arrayName at[" . $key . "] is: [" .$value . "]<br>\n";
foreach($key=>$value的行)
echo“位于[“$key.”的arrayName是:[“$value.”]
\n;
您正在使用mysql获取\u fetch\u assoc so
将循环更改为
foreach($row as $key => $value){
echo "Array key : $key = $value <br/>";
}
foreach($key=>$value的行){
echo“数组键:$key=$value
”;
}
您正在使用mysql获取\u fetch\u assoc so
将循环更改为
foreach($row as $key => $value){
echo "Array key : $key = $value <br/>";
}
foreach($key=>$value的行){
echo“数组键:$key=$value
”;
}
使用mysql\u fetch\u array()而不是mysql\u fetch\u assoc()
mysql_fetch_assoc()将返回一个关联数组,只能通过$row['name']访问。
使用mysql_fetch_array()可以获取关联数组、数字数组或两者
看看这里:使用mysql\u fetch\u array()而不是mysql\u fetch\u assoc()
mysql_fetch_assoc()将返回一个关联数组,只能通过$row['name']访问。
使用mysql_fetch_array()可以获取关联数组、数字数组或两者
请看这里: