如何在循环内将关联数组附加到另​​一个数组中-php [英] how to append associative arrays into another array inside loop - php

查看:59
本文介绍了如何在循环内将关联数组附加到另​​一个数组中-php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望你能帮助我解决这个问题,希望这对你有帮助-我有一个foreach循环的伪示例:

I really hope you help me with this problem, I hope this make sence for you - I have this pseudo example of foreach loop:

foreach_loop {

$k1 = GetKey1(); 
$v1 = GetValue1();

$k2 = GetKey2();
$v2 = GetValue2();

$k3 = GetKey3();
$v2 = GetValue3();

//  now I put those keys and values in associative array called DataArr
 $DataArr[$k1] = $v1;
 $DataArr[$k2] = $v2;
 $DataArr[$k3] = $v3;

}

现在我的问题是,如何创建一个数组,其中每个索引包含一个从该 foreach 循环创建的关联数组,并像这样继续附加自身:

now my question is, how do I create an array where each index of it contain an associative array created from that foreach loop and keep appending to itself like this:

     $resultArr = array(
     0 => "DataArr_from_loop1",
     1 => "DataArr_from_loop2",
     2 => "DataArr_from_loop3",
     3 => "DataArr_from_loop4"
     //...etc
     )

,当我检查 $ resultArr [0] 时,我应该得到一个像这样的关联数组:

and when I check for $resultArr[0] I should get an associative array like this:

    array (size=3)
    'k1' => string 'v1'
    'k2' => string 'v2'
    'k3' => string 'v3'

我真的需要你的帮助,谢谢.

I really need your help, thank you in advance.

推荐答案

如何...

$resultArr = array();

foreach($whatever as $thing) {

  $k1 = GetKey1(); 
  $v1 = GetValue1();

  $k2 = GetKey2();
  $v2 = GetValue2();

  $k3 = GetKey3();
  $v2 = GetValue3();

  //  now I put those keys and values in associative array called DataArr
  $DataArr = array();  
  $DataArr[$k1] = $v1;
  $DataArr[$k2] = $v2;
  $DataArr[$k3] = $v3;

  $resultArr[] = $DataArr;

}

这篇关于如何在循环内将关联数组附加到另​​一个数组中-php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆