PHP数组到字符串转换错误

时间:2022-09-22 16:31:54

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

注意:第72行的C:\ xampp \ htdocs \ usa \ catalog \ view \ theme \ usadevims \ template \ product \ compare.tpl中的数组到字符串转换ArrayNotice:C:\ xampp \ htdocs \ usa \ catalog中的数组到字符串转换第72行的\ view \ theme \ usadevims \ template \ product \ compare.tplArrayNotice:第72行的C:\ xampp \ htdocs \ usa \ catalog \ view \ theme \ usadevims \ template \ product \ compare.tpl中的数组到字符串转换

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"
      }
    }

1 个解决方案

#1


$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变量分配一个数组。

#1


$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变量分配一个数组。