doutu1889 2014-04-04 12:18
浏览 23
已采纳

如何在循环中的复杂数组的最低级别设置数组条目?

Ok, I admit the question is confusing but I don't know how to say it in another way. I have an array that looks like this:

array(3) {
  [0]=>
  array(1) {
    [some_middle-level_id]=>
    array(1) {
      [some_low-level_id]=>
      array(5) {
        ["entry1"]=>
        string(...) "..."
        ["entry2"]=>
        string(...) ""
        ...
      }
    }
  }  
  [1]=>
  array(1) {
    [another_middle-level_id]=>
    array(5) {
      [another_low_level_id]=>
      array(5) {
        ["entry1"]=>
        string(...) "..."
        ["entry2"]=>
        string(...) ""
        ...
      }
    }
  }
  [2]=>
  array(1) {
    [another_middle-level_id]=>
    array(5) {
      [another_low_level_id]=>
      array(5) {
        ["entry1"]=>
        string(...) "..."
        ["entry2"]=>
        string(...) ""
        ...
      }
      [another_low_level_id]=>
      array(5) {
        ["entry1"]=>
        string(...) "..."
        ["entry2"]=>
        string(...) ""
        ...
      }
    }
  }

EDIT: The other array would look like this. Let's say there are 3 [low_level_id] in the first array then the other array would simply look like this:

array(3) { [0]=> string(3) "bla" [1]=> string(4) "blub" [2]=> string(6) "lalala"}

and another array that have as many entries as "low level id"-entries exist. In every "low level id"-entry which contains 5 entries at the moment I want to add a sixth entry - a string from the other array. But I don't know how to access it. Is there away to access it without using hundreds of loops?

Another EDIT: I could access the first entry with:

foreach($my_array as $key => $subkey){
    foreach($subkey as $val){
         $my_array[$key][key($subkey)][key($val)]['6th_entry']="a new string";
    }
}

But that works only for the first new entry. I don't know how to access the others and how to add a loop for adding entries from the other array.

  • 写回答

1条回答 默认 最新

  • dongshao1873 2014-04-04 12:53
    关注

    You can use nested calls of array_map. Assuming $array is 1st array and $another_array is 2nd array, the code should look like this:

    $result = array_map(
        function ($elem1) use(&$another_array) {
            return array_map(
                function ($elem2) use(&$another_array) {
                    return array_map(
                        function ($elem3) use(&$another_array) {
                            list(, $val) = each($another_array);
                            $elem3['entry6'] = $val;
                            return $elem3;
                        },
                        $elem2
                    );
                },
                $elem1
            );
        },
        $array
    );
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?