迭代表列并获得结果

时间:2015-12-15 13:43:03

标签: php mysql

我有$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; ?>

但逻辑并不好,我明白为什么不行,但我无法弄清楚如何循环columnsproducts

1 个答案:

答案 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; ?>