我正在制作发票原型(prototype)以供练习。我使用的是 CakePHP 3,项目总是会有所不同,因此将它们与另一个表中的行关联起来是行不通的。在每一行中,我都有一个名为 items 的列,其中有一个数组,例如:
array(
array("Name" => "apples",
"Desc" => "a fruit called apple",
"Price" => "1"
),
array("Name" => "orange",
"Desc" => "oranges",
"Price" => "2",
),
array("Name" => "watermelon",
"Desc" => "watermelon",
"Price" => "6"
)
);
我想使用该数组构建一个 foreach 循环来显示发票中的所有项目。
foreach ($items as $i => $row) {
echo $row['Name'] . $row['Price'] . $row['Desc'] . "<br>";
}
我正在尝试使用此方法从列中获取数据
<?php $items = $billing->items; ?>
但这就是我收到的:
Warning (2): Invalid argument supplied for foreach() [APP/Template\Billing\invoice.ctp, line 56]
如果我尝试回显数据,它会回显正常。我不明白为什么我不能将其设为变量。
<?= h($billing->items); ?>
最佳答案
Cake 仅为模型关联创建实体,而不是列。在这种情况下,如果您必须使用某种排序的 Collection
方法或 php array_*
函数手动设置属性。
编辑:
如果您要存储为序列化数组,则必须先对其进行反序列化
关于php - 数据库中的数组,用作变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803692/