多个阵列foreach语句

问题描述:

我从我现有的HTML工作在我的第一个PHP布局。我遇到了麻烦。我希望计算器可以帮助多个阵列foreach语句

我有3个阵列

$parent = array('1' => 'parent1' , 'parent2' , 'parent3' , 'parent4' ,); 
$child = array('1' => 'child1' , 'child2' , 'child3' , 'child4' ,); 
$content = array('1' => 'content1' , 'content2' , 'content3' , 'content4' ,); 

我试图用它们来进行重复名单,其中将包括{$父},{$子}和{$内容}像以下...

<?php 
    echo "<h1 id='js-overview' class='page-header'>$parent</h1>" 
    echo "<h3 id='js-fotopia'>$child</h3>" 
    echo "<p>$content</p>" 
?> 

但我需要为我的每个变量运行此。我如何撰写声明?还我怎么能设定,让孩子和孩子的内容也被重复。像PIC

见PIC理解我的布局,我需要保持,只要有填写变量重复...请提问,如果我不是因为做而不是跳跃......我需要的PHP指导。

never mind the menu

enter image description here

也许这样的事情?

for($i=1; $i<=count($parent); $i++){ 
     echo "<h1 id='js-overview' class='page-header'>$parent[$i]</h1>" 
     echo "<h3 id='js-fotopia'>$child[$i]</h3>" 
     echo "<p>$content[$i]</p>" 
} 

或者,您也可以使用foreach并假设键排队这样的语法

foreach (array_expression as $key => $value) statement

或者到你可以构建1个二维数组存储的父母,子女和内容都在1排。

我想你的结构是有点过。为什么不把它们合并成一个,而不是3个数组?这是一个有点不清楚的父/子/内容你有关系是如何建立的,但这里有一个镜头:

$arr = array(
    'parent1' => array(
     'child1' => array(
      'content' => 'This is your content for child 1', 
     ), 
     'child2' => array(
      'content' => 'This is your content for child 2', 
     ), 
    ), 
    'parent2' => array(
     'child1' => array(
      'content' => 'This is your content for child 1 inside of parent 2', 
     ) 
    ), 
); 

那么你可以foreach在阵列,并foreach过孩子:

foreach($arr as $parent) { 
    foreach($parent as $childName => $child) { 
     echo $child['content']; 
    } 
} 

如果你需要更多的结构到您的内容,您可以添加多个键进入儿童阵列。

+0

感谢...使用一个版本的这个...我有办法在PHP去 – Omar