Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP中的单个循环回显分离的html节_Php_Html_Loops - Fatal编程技术网

从PHP中的单个循环回显分离的html节

从PHP中的单个循环回显分离的html节,php,html,loops,Php,Html,Loops,在处理必须回显到分离的html节的循环时,PHP中的常见模式是什么 假设我有一个cart()函数,它以数组的形式返回产品项。例如,产品: $aProduct = ['details' => 'this details', 'frames' => 2, 'price' => 58] 现在,我必须遍历cart(),在不同的html部分中回显每个产品的数据 <section class="details"> //each product

在处理必须回显到分离的html节的循环时,PHP中的常见模式是什么

假设我有一个cart()函数,它以数组的形式返回产品项。例如,产品:

$aProduct = ['details' => 'this details', 'frames' => 2, 'price' => 58]
现在,我必须遍历cart(),在不同的html部分中回显每个产品的数据


  <section class="details">
    //each product's details
  </section>

  <section class="frames">
    // each product's frames
  </section>

  <section class="price">
    // each product's price
  </section>


//每个产品的详细信息
//每个产品的框架
//每种产品的价格
…将整个结构围成一个环是简洁的,但会重复不需要的部分:

<?php foreach(cart() as $product): ?>

  <section class="details">
    <?= $product['details'] ?>
  </section>

  <section class="frames">
    <?= $product['frames'] ?>
  </section>

  <section class="price">
    <?= $product['price'] ?>
  </section>

<?php endforeach ?>

…看来唯一的选择是在每个部分内复制循环:

  <section class="details">
    <?php foreach(cart() as $product): ?>
      <?= $product['details'] ?>
    <?php endforeach ?>
  </section>

  <section class="frames">
    <?php foreach(cart() as $product): ?>
      <?= $product['frames'] ?>
    <?php endforeach ?>
  </section>

  <section class="price">
    <?php foreach(cart() as $product): ?>
      <?= $product['price'] ?>
    <?php endforeach ?>
  </section>

…但这是重复的,当数据变得更复杂时,可能会使事情变得困难,例如嵌套数组,其元素需要在节之间同步弹出

在这种情况下,是否有PHP替代方案、最佳实践或通用模式,或者在HTML结构中可能更好地解决这一问题


谢谢你

一个很好的经验法则是避免模板中可以避免的任何逻辑。要实现类似的目标,请始终以所需的确切形式为视图准备数据。这将使您的模板简单明了

因此,与其采用包含所有节数据的单个数组,不如:

[
['details'=>this details','frames'=>2,'price'=>58],
['details'=>'那些细节','frames'=>4,'price'=>69],
['details'=>that details','frames'=>6,'price'=>93],
]
重复多次只为了得到所需的分数,应该将其拆分为多个数组,保存单个部分的所有细节,因此最终得到如下结果:

$details=['这个细节'、'那些细节'、'那个细节'];
$frames=[2,4,6];
美元价格=[58,69,93];
之后,每个部分的输出都很简单

此示例非常简单,因此
array\u column
可以将购物车数组分离为上面介绍的单个数组。如果cart数组结构稍微复杂一点,则可能必须应用自定义函数