我有一个下面给出的多维数组

时间:2014-09-26 10:35:06

标签: php arrays multidimensional-array foreach

我有一个像这样的数组

$x = array("1" =>array("PHP","code"), "foo" =>array("bar", 5 ,11));

for($i=1;$i<count($x);$i++)
{
    foreach($x[$i] as $key=>$value){
        echo $key.$value;
    }
echo "<br>";
}

我希望输出如下: 1.php 1.code foo.bar foo.5 foo.11

我怎么能得到这个。

4 个答案:

答案 0 :(得分:1)

你快到了:

foreach ($x AS $key => $val) {
  foreach ($val AS $sub) {
    echo $key . '.' . $sub . '<br>';
  }
}

答案 1 :(得分:1)

循环遍历每个数组,如此;

<?php

$x = array("1" =>array("PHP","code"),  
           "foo" =>array("bar", 5 ,11)
          );

foreach($x as $key => $a) {
   foreach($a as $c) {
     echo $key . "." . $c . PHP_EOL;
   }
}

https://eval.in/198869

答案 2 :(得分:0)

<?php

$x = array("1" =>array("PHP","code"), "foo" =>array("bar", 5 ,11));

foreach($x as $m=>$n){
    foreach($n as $y=>$x){

       $arrayList[]= $m.".".strtolower($x);
     }
}

echo "<pre>";
print_r($arrayList);
echo "</pre>";
?>

答案 3 :(得分:0)

您必须将换行符<br>放在内部循环中,如下所示:

$x = array( "1" => array("PHP","code"), "foo" => array("bar", 5 ,11));

foreach($x as $key=>$val){

     foreach($val as $value){
         echo $key.".".$value."<br>";
     }

}