我创建了一个有4级的超级阵列。
这是野兽:
array(4) {
["arrayCoordinateur"]=>
array(2) {
["siret"]=>
array(1) {
[0]=>
string(14) "44306184100039"
}
["sigle"]=>
array(1) {
[0]=>
string(3) "NdP"
}
}
["arrayGroupMember"]=>
array(2) {
["siret"]=>
array(2) {
[0]=>
string(14) "44306184100039"
[1]=>
string(14) "44306184100039"
}
["sigle"]=>
array(2) {
[0]=>
string(7) "rerzrez"
[1]=>
string(5) "Autre"
}
}
["arrayPartEnt"]=>
array(2) {
["sigle"]=>
array(3) {
[0]=>
string(6) "Blabla"
[1]=>
string(11) "CharbonBleu"
[2]=>
string(3) "JsP"
}
["siret"]=>
array(3) {
[0]=>
string(14) "77408201000034"
[1]=>
string(14) "51133834500024"
[2]=>
string(14) "40794236600011"
}
}
["arrayPartenExt"]=>
array(2) {
["sigle"]=>
array(2) {
[0]=>
string(3) "BNN"
[1]=>
string(5) "13456"
}
["siret"]=>
array(1) {
[0]=>
string(14) "00000000000000"
}
}
}
正如您所看到的,该结构由4个阵列组成,其中所有子阵列都被称为" siret"和" sigle"。
我希望能够只在那些siret和sigle上循环,而不会被第一级烦恼,把它们放到一张桌子里。
这是我的表:
$i = 0;
foreach ($allPartenaires as $partenaire=>$data) {
for ($i=0; $i < count($data) ; $i++) {
echo "<tr>
<td>
".$data['sigle'][$i]."
</td>
<td>
".$data['siret'][$i]."
</td>
</tr>";
}
}
$ allPartenaires是本文开头的超级数组。
此代码不起作用,因为所有子阵列都没有相同的大小...
非常感谢任何帮助
答案 0 :(得分:1)
我认为这就是你所需要的:
$data = array(
"arrayCoordinateur"=>array(
"siret"=>array(
"44306184100039",
),
"sigle"=>array(
"NdP",
),
),
"arrayGroupMember"=>array(
"siret"=>array(
"44306184100039",
"44306184100039",
),
"sigle"=>array(
"rerzrez",
"Autre",
),
),
"arrayPartEnt"=>array(
"siret"=>array(
"77408201000034",
"51133834500024",
"40794236600011",
),
"sigle"=>array(
"Blabla",
"CharbonBleu",
"JsP",
"Something",
),
),
);
echo '<table>';
foreach($data as $key => $value) {
$length = count($value['siret']) > count($value['sigle']) ? count($value['siret']) : count($value['sigle']);
for($i = 0; $i < $length; $i++) { //Now it works even if siret and single doesn't have same number of elemenets
$v1 = '-';
$v2 = '-';
if (isset($value['siret'][$i])) {
$v1 = $value['siret'][$i];
}
if (isset($value['sigle'][$i])) {
$v2 = $value['sigle'][$i];
}
echo
'
<tr>
<td>'.$v1.'</td>
<td>'.$v2.'</td>
</tr>
';
}
}
echo '</table>';
结果:
44306184100039 NdP
44306184100039 rerzrez
44306184100039 Autre
77408201000034 Blabla
51133834500024 CharbonBleu
40794236600011 JsP
- Something
答案 1 :(得分:0)
嵌套的foreach循环是你需要的
foreach ($allPartenaires as $data) {
echo '<tr>';
foreach($data as $nestedArray) {
foreach($nestedArray as $content) {
echo "<td>{$content}</td>";
}
}
echo '</tr>';
}