php PHP数组到字符串转换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30128656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Array to string conversion error
提问by Kundan SIngh
I am trying to output some content from my DB table, i successfully made a query and also return controller code but when i am trying ouput it in my view, what i tried
我正在尝试从我的数据库表中输出一些内容,我成功进行了查询并返回了控制器代码,但是当我尝试在我的视图中输出它时,我尝试了什么
<tr>
<?php foreach ($products as $product) { ?>
<td>
<pre>
<?php
var_dump($products[$product['product_id']]['manufacturers']);
foreach ($products[$product['product_id']]['manufacturers'] as $manufacturer) {
echo $manufacturer;
} ?>
</pre>
</td>
<?php } ?>
</tr>
ERROR
Notice: Array to string conversion in C:\xampp\htdocs\usa\catalog\view\theme\usadevims\template\product\compare.tpl on line 72ArrayNotice: Array to string conversion in C:\xampp\htdocs\usa\catalog\view\theme\usadevims\template\product\compare.tpl on line 72ArrayNotice: Array to string conversion in C:\xampp\htdocs\usa\catalog\view\theme\usadevims\template\product\compare.tpl on line 72Array
错误
注意:C:\xampp\htdocs\usa\catalog\view\theme\usadevims\template\product\compare.tpl 中的数组到字符串转换 72Array 注意:数组到字符串的转换在 C:\xampp\htdocs\usa\catalog \view\theme\usadevims\template\product\compare.tpl on line 72ArrayNotice: Array to string conversion in C:\xampp\htdocs\usa\catalog\view\theme\usadevims\template\product\compare.tpl on line 72Array
and here the var_dump of my variable
这里是我的变量的 var_dump
array(3) {
[0]=>
array(2) {
["name"]=>
string(5) "Apple"
["manufacturer_id"]=>
string(1) "8"
}
[1]=>
array(2) {
["name"]=>
string(3) "HTC"
["manufacturer_id"]=>
string(1) "5"
}
[2]=>
array(2) {
["name"]=>
string(4) "Sony"
["manufacturer_id"]=>
string(2) "10"
}
}
回答by Gabriel Moretti
$manufacturer references to an array. Try:
$manufacturer 对数组的引用。尝试:
echo $manufacturer['name'];
or
或者
echo $manufacturer['manufacturer_id];
As you can see on your var_dump
, your variable $products[$product['product_id']]['manufacturers']
is an array composed by three other arrays. So each iteration of your loop will assign an array to $manufacturer variable.
正如您在 上看到的var_dump
,您的变量$products[$product['product_id']]['manufacturers']
是一个由其他三个数组组成的数组。因此,循环的每次迭代都会为 $manufacturer 变量分配一个数组。