如何将数组的值合并为多维数组?

时间:2015-09-01 16:49:09

标签: php arrays multidimensional-array

对不起我的问题标题我真的不知道如何强调我的问题,但这里是样本数组:

enter image description here

这是我的代码

foreach ($_POST['noofguest'] as $keyg => $valueg) {

                echo $valueg. "<br />"; 

}

        foreach($_POST['room_no'] as $key => $value){

            foreach($value as $key2 => $value2){

                echo $value2 . " has " . $valueg . "<br />";
            }


        }

,结果是:

1

2

56 has 1

57 has 1

但这不是我想要的结果,我想要的是

1

2

56 has 1

57 has 2

这可能吗?

1 个答案:

答案 0 :(得分:1)

对于第二个循环,您希望接收room_no的相同索引,但仅在noofguest数组内。这就是你应该做的事情:

foreach($_POST['room_no'] as $key => $value)
  foreach($value as $key2 => $value2)
    echo $value2 . " has " . $_POST['noofguest'][$key2] . "<br />";