Php 循环遍历数组并按索引添加到另一个数组

Php 循环遍历数组并按索引添加到另一个数组,php,arrays,multidimensional-array,rotation,transpose,Php,Arrays,Multidimensional Array,Rotation,Transpose,我有一个如下所示的数组: array(8) { ["rentalPropertyAddress"]=> array(15) { [0]=> string(11) "111 tree st" [1]=> string(11) "112 tree st" [2]=> string(11) "122 tree st" } ["gasInitialized"]=> array(15) { [0]=&

我有一个如下所示的数组:

array(8) {
  ["rentalPropertyAddress"]=>
  array(15) {
    [0]=>
    string(11) "111 tree st"
    [1]=>
    string(11) "112 tree st"
    [2]=>
    string(11) "122 tree st"
  }
  ["gasInitialized"]=>
  array(15) {
    [0]=>
    string(2) "on"
    [1]=>
    string(2) "on"
    [2]=>
    string(3) "off"
  }
  ["waterInitialized"]=>
  array(15) {
    [0]=>
    string(3) "off"
    [1]=>
    string(2) "on"
    [2]=>
    string(2) "on"
  }
  ["electricInitialized"]=>
  array(15) {
    [0]=>
    string(2) "on"
    [1]=>
    string(2) "on"
    [2]=>
    string(3) "off"
  }
  ["inspectionDate"]=>
  array(15) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  ["rentalDate"]=>
  array(15) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  ["vacantInitialized"]=>
  array(15) {
    [0]=>
    string(2) "no"
    [1]=>
    string(2) "no"
    [2]=>
    string(3) "yes"
  }
}
我需要做的是将每个数组的每个索引添加到另一个或多个数组中。例如,预期输出为:

array {
  array {
    [0] => string(11) "111 tree st"
    [1] => string(2) "on"
    [2] => string(3) "off"
    [3] => string(2) "on"
    [4] => string(0) ""
    [5] => string(0) ""
    [6] => string(2) "no"
  }
  ...
}
我已经尝试使用forloop来实现这一点,方法是在数组中循环,并保留和索引它:

$i = -1;
$retval = array();
foreach ($_GET as $key => $item) {
    $i += 1;
    $retval[$i] = $item[$i];
}
echo "<pre>";var_dump($retval);

如何成功地将数据从数组中提取到不同的数组中?

如果我没有误解您所需的输出,那么您可以使用两个
foreach()
迭代每个嵌套数组,并按所有嵌套数组的索引位置推送每个元素

foreach ($a as $row) {
    foreach ($row as $col => $value) {
        $result[$col][] = $value;
    }
}
$\u GET=[
“rentalPropertyAddress”=>[“111号树街”、“112号树街”、“122号树街”],
“已初始化”=>[“打开”、“打开”、“关闭”],
“waterInitialized”=>[“关闭”、“打开”、“打开”],
“电气初始化”=>[“开”、“开”、“关”],
“检查日期”=>,
“rentalDate”=>,
“VacanInitialized”=>[“否”、“否”、“是”]
];
$retval=[];
foreach($\u获取为$key=>$item){
$i=0;
foreach($k=>v的项目){
$retval[$i][]=$v;
$i++;
}  
}
回声“;
打印(返回);
回声“;

演示:

将列切换为行的一般模式是:

<input type="text" name="properties[0][rentalPropertyAddress]">
<input type="text" name="properties[1][rentalPropertyAddress]">
但是,您可以在superglobal中获得此结果,而无需使用PHP将表单输入(假设数据来自表单)重命名为

/*The initial array : I chose this one as it is simpler than the one in the question and I find it better for illustration purposes*/
/*Considering the question, we will only get the values ended by 0 (first value of each subarray )*/
$arrays = [['00','01'],['10','11'],['20','11']];

/*Initialisation of the result array*/
$indexes = [];

/*We are going to use the new syntax for list() function introduced in php 7.1  (See the credits link for list() function usage examples )*/

/* We pull the first value (index 0) of each subarray that we store in the result array ($indexes) */

foreach($arrays as $array){ 
    list('0'=>$indexes[]) = $array; 
}

var_dump($indexes); // ['00','10','20']

改为这种格式

var_export(array_map(null,...array_values($_GET)));


即使您使用JS动态添加行,这仍然是可能的。

我建议对运行从7.1到7.3的php版本的用户执行以下操作(例如,请参阅演示链接):

学分:


如果你穿着袜子,你可能想紧紧抓住它们。您正在寻找的技术称为“数组转置”。只要删除外部关联键,
array\u map()
和splat操作符(
)就会快速准备数据

是的,这真的很容易

唯一比这更容易的方法是,遵循Don'tPanic的建议,在html表单中准备好数据结构

输出:


是这样的,但是我需要对数组中的每个索引都这样做,请原谅我的措辞,我不是一个php开发者,但它是以key=>value的方式对每个索引这样做的。如果我遗漏了什么,请告诉我。试一下我的示例代码,让我看看输出是什么?是的,它完全符合您所说的。但它仅对第一个索引执行此操作:
array(7){[“rentalPropertyAddress”]=>string(11)“111 tree st”[“gasInitialized”]=>string(2)”在“[“waterInitialized”]=>string(3)”上关闭“[“electricInitialized”]=>string(2)”在“[“inspectionDate”]=>string(0)”[“rentalDate”]=>string(0)”[“VacanInitialized”=>string”=>string(0)”(2) “否”}
@saladCracker噢,我的错,我现在得到了您期望的输出。请查看我的编辑。希望它现在对所有索引都有效:)呃。。。刚刚意识到splash已经评论过了。现在我感觉不那么酷了。我注意到我的答案被否决了,我想知道我错过了什么……在浏览了一些被否决的答案后,我发现我上面的答案应该包括更多的解释。我忽略了这一点,因为我提供了到原始文章的链接。不过,我会尝试在将来给出更好的答案和更多的解释。我很高兴知道更多关于我做错的事情,不管你是否投了反对票。
<input type="text" name="properties[0][rentalPropertyAddress]">
<input type="text" name="properties[1][rentalPropertyAddress]">
/*The initial array : I chose this one as it is simpler than the one in the question and I find it better for illustration purposes*/
/*Considering the question, we will only get the values ended by 0 (first value of each subarray )*/
$arrays = [['00','01'],['10','11'],['20','11']];

/*Initialisation of the result array*/
$indexes = [];

/*We are going to use the new syntax for list() function introduced in php 7.1  (See the credits link for list() function usage examples )*/

/* We pull the first value (index 0) of each subarray that we store in the result array ($indexes) */

foreach($arrays as $array){ 
    list('0'=>$indexes[]) = $array; 
}

var_dump($indexes); // ['00','10','20']
var_export(array_map(null,...array_values($_GET)));
array (
  0 => 
  array (
    0 => '111 tree st',
    1 => 'on',
    2 => 'off',
    3 => 'on',
    4 => '',
    5 => '',
    6 => 'no',
  ),
  1 => 
  array (
    0 => '112 tree st',
    1 => 'on',
    2 => 'on',
    3 => 'on',
    4 => '',
    5 => '',
    6 => 'no',
  ),
  2 => 
  array (
    0 => '122 tree st',
    1 => 'off',
    2 => 'on',
    3 => 'off',
    4 => '',
    5 => '',
    6 => 'yes',
  ),
)