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

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改