首页 > 解决方案 > 在 codeigniter 中查看数组到字符串的转换

问题描述

我不明白为什么我的变量 $product_quantity 出现错误,而它的 $var_dump($product_quantity) 给出了元素。代码片段中指示的错误点为第 63 行。此外,它没有得到 $productInfo['image'] 和 $productInfo['url']。在此处检查 $var_dump($product_quantity) 的输出

<?php
$arr_products = unserialize($order['products']);

foreach($arr_products as $product_id => $product_quantity)
{
    $productInfo = modules::run('admin/ecommerce/products/getProductInfo', $product_id, true);
    var_dump($product_quantity);
    ?>
    <div style="word-break: break-all;">
        <div>
            <img src="<?= base_url('attachments/shop_images/'.$productInfo['image']) ?>" alt="Product" style="width:100px; margin-right:10px;" class="img-responsive">
        </div>
        <a target="_blank" href="<?= base_url($productInfo['url']) ?>">
            <?= base_url($productInfo['url']) ?> 
        </a> 
        <--line 63-->
        <div style=" background-color: #f1f1f1; border-radius: 2px; padding: 2px 5px;"><b><?= lang('user_order_quantity') ?></b> <?= $product_quantity ?></div>

    </div>

    <hr>
<?php }
?>

标签: phpcodeigniter

解决方案


如您var_dump的数组所示,它是一个多级关联数组,其父键为product_infoand product_quantity

我怀疑如果您将有问题的行更改为<?php echo $product_quantity['product_quantity']; ?>您将不再有错误。

此外,您似乎也$product_quantity包含您的内容product_info,所以我不确定这是否完全有必要$productInfo = modules::run('admin/ecommerce/products/getProductInfo', $product_id, true); (不知道您的整个系统,所以我可能错了)


推荐阅读