问题描述
<?php
$products = array();
$products[101] = array(
"name" => "Logo Shirt, Red",
"price" => 18,
"img" => "img/shirts/shirts-101.jpg"
);
echo $products;
?>
我正在尝试运行此php文件,并且一直在给我这个错误- 注意:第8行数组在C:\ xampp \ htdocs \ example \ echo.php中将数组转换为字符串.
I am trying to run this php file and it keeps giving me this error - Notice: Array to string conversion in C:\xampp\htdocs\example\echo.php on line 8 Array.
我要做的就是回显数组中的每个元素.我也尝试过
All i want to do is echo out each and every element inside the array.I've also tried
<?php
$products = array();
$products[101] = array(
"name" => "Logo Shirt, Red",
"price" => 18,
"img" => "img/shirts/shirts-101.jpg"
);
foreach($products as $product){
echo $product;
}
?>
好的,如果有多个类似的数组,该怎么办 $产品[101] $ product [102] $ product [103] $ product [104] . . . $ product [n]
okay guys what if there are multiple similar arrays like $product[101] $product[102] $product[103] $product[104] . . . $product[n]
那又是什么?
推荐答案
如果您echo
一个数组,它将给您一个错误,或者仅输出"Array".
If you echo
an array, it will either give you an error or just output "Array".
在第二个示例中,$products[101]
也是也是数组.
In your second example, $products[101]
is also an array.
因此您可以执行以下操作:
So you can do this:
foreach($products as $product){
foreach($product as $key => $val){
echo "$key: $val";
}
}
或使用print_r
将数据输出为数组:
Or use print_r
to output the data in an array:
foreach($products as $product){
print_r($product);
}
这篇关于注意:在第8行数组C:\ xampp \ htdocs \ example \ echo.php中将数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!