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

时间:2018-11-02 15:37:49

标签: 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]=>
    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);

但是输出不是我期望的:

array(8) {
  [0]=>
  string(11) "111 tree st"
  [1]=>
  string(2) "on"
  [2]=>
  string(2) "on"
  [3]=>
  string(3) "off"
  [4]=>
  string(0) ""
  [5]=>
  string(0) ""
  [6]=>
  string(3) "yes"
  [7]=>
  string(1) "5"
}

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

4 个答案:

答案 0 :(得分:0)

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

   $_GET = [
         "rentalPropertyAddress"=>["111 tree st","112 tree st","122 tree st"],
         "gasInitialized"=>["on","on","off"],
         "waterInitialized"=>["off","on","on"],
         "electricInitialized"=>["on","on","off"],
         "inspectionDate"=>["","",""],
         "rentalDate"=>["","",""],
         "vacantInitialized"=>["no","no","yes"]
];

$retval = [];
foreach ($_GET as $key => $item) {
    $i=0;
    foreach($item as $k=>$v){
      $retval[$i][] = $v;
      $i++;
    }  
}
echo "<pre>";
print_r($retval);
echo "</pre>";

演示: https://3v4l.org/LrpGo

答案 1 :(得分:0)

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

foreach ($a as $row) {
    foreach ($row as $col => $value) {
        $result[$col][] = $value;
    }
}

但是,您可以通过以下方式重命名表单输入(假设数据来自表单),而无需使用PHP即可将其转换为超全局结果

<input type="text" name="rentalPropertyAddress[]">
<input type="text" name="rentalPropertyAddress[]">

采用这种格式

<input type="text" name="properties[0][rentalPropertyAddress]">
<input type="text" name="properties[1][rentalPropertyAddress]">

即使使用JS动态添加行,这仍然应该可行。

答案 2 :(得分:0)

如果您穿着袜子,可能要挂在袜子上。您正在寻找的技术称为“数组转置”。只要您删除外部关联键,array_map()和splat运算符(...)就会快速准备数据。

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

是的,确实很容易。 Demo

要比这更容易的唯一方法是,如果您遵循Don'tPanic的建议并以html表单重新准备数据结构。

输出:

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',
  ),
)

答案 3 :(得分:-1)

我建议对运行7.1至7.3的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']

信用:The list function & practical uses of array destructuring in PHP

Demo