我有$columns
数组
Array
(
[0] => stdClass Object
(
[Field] => category_id
)
[1] => stdClass Object
(
[Field] => category_name
)
[2] => stdClass Object
(
[Field] => category_slug
)
)
和$products
数组
Array
(
[0] => stdClass Object
(
[category_id] => 1
[category_name] => Articulations
[category_slug] => articulations
)
[1] => stdClass Object
(
[category_id] => 2
[category_name] => Card
[category_slug] => card
)
[2] => stdClass Object
(
[category_id] => 3
[category_name] => Bar
[category_slug] => bar
)
[3] => stdClass Object
(
[category_id] => 4
[category_name] => FOO
[category_slug] => foo
)
)
,其中包含列值。
如何遍历列并获取每列的结果?
我尝试过:
<? foreach($columns as $column): ?>
<tr data-id="">
<?php foreach($produse as $produs): ?>
<td><?= $produs->{$column->Field} ?></td>
<?php endforeach; ?>
</tr>
<? endforeach; ?>
但逻辑并不好,我明白为什么不行,但我无法弄清楚如何循环columns
和products
。
答案 0 :(得分:3)
您似乎只是缺少引用Field
个对象的column
属性:
<? foreach($columns as $column): ?>
<tr data-id="">
<?php foreach($produse as $produs): ?>
<td><?= $produs->{$column->Field} ?></td>
<?php endforeach; ?>
</tr>
<? endforeach; ?>