循环遍历数组并通过索引添加到另一个数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环遍历数组并通过索引添加到另一个数组相关的知识,希望对你有一定的参考价值。

我有一个看起来像这样的数组:

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"
  }
  ...
}

我已经尝试通过循环遍历数组并保持和索引来执行for循环:

$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"
}

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

答案

如果我没有误解你所需的输出,那么你可以使用两个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>";

但是:ぁzxswい

另一答案

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

https://3v4l.org/LrpGo

但是,您可以在超全局中获得此结果,而无需通过重命名表单输入(假设数据来自表单)从类似于

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

这种格式

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

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

另一答案

如果你穿着袜子,你可能想要挂在上面。您正在寻求的技术称为“阵列转置”。只要删除外部关联键,<input type="text" name="properties[0][rentalPropertyAddress]"> <input type="text" name="properties[1][rentalPropertyAddress]"> 和splat运算符(array_map())就会快速准备数据。

...

是的,这真的很容易。 var_export(array_map(null,...array_values($_GET)));

它比这更容易的唯一方法是,如果你按照LeavePanic的建议,并以html格式准备你的数据结构。

输出:

Demo
另一答案

对于那些从7.1到7.3运行php版本的人,我建议做以下事情(例如参见演示链接):

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

致谢:/*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

以上是关于循环遍历数组并通过索引添加到另一个数组的主要内容,如果未能解决你的问题,请参考以下文章

MASM 数组不循环

如何通过设置寻呼机循环遍历 Java 数组列表条目?

循环遍历数组以获取项目的索引 - swift

数组的遍历

汇编学习:二维数组遍历

python运算学习之Numpy ------ 数组的切片索引与循环遍历条件和布尔数组