doufang7385 2016-08-24 17:18
浏览 23
已采纳

如何使用递归构建新数组时“跳过”迭代

I am stuck on something that might be very simple.

I am creating a new array by looping through an existing array using a recursion function yet I can not seem to get the values to stick to the new array. The function, in the end, will be a bit more complex, but for now I need some help.

I have tried soooo many ways to get this to work but I am at a loss right now.

Here is my php function

function recursive($array) {

    $newArray = array();

    foreach($array as $key => $value) {

        if(is_array($value)){
            recursive($value);                              
        }else{
            $newArray[] = $value;
        }   

    }

    return $newArray;

}

As is, the new array never gets filled...BUT, if I change this line...

recursive($value); // Why can't I just call the recursive function here?

...to...

$newArray[] = recursive($value); // Instead of having to set a new value to the new array?

everything works properly...except that my goal was to create a flat array with only the values.

So my question is, why is it necessary to set a new array value in order to call the recursive function again? Ideally, I want to skip setting a new array value if the value is an array and just continue the loop through the original array.

  • 写回答

3条回答 默认 最新

  • dongmoxin7111 2016-08-24 17:24
    关注

    Use array_merge:

    function recursive($array) {
    
        $newArray = array();
    
        foreach($array as $key => $value) {
    
            if(is_array($value)){
                $newArray = array_merge($newArray, recursive($value));                              
            }else{
                $newArray[] = $value;
            }   
    
        }
    
        return $newArray;
    
    }
    

    ...or you could use special operator:

    function recursive($array) {
    
        $newArray = array();
    
        foreach($array as $key => $value) {
    
            if(is_array($value)){
                $newArray += recursive($value);                              
            }else{
                $newArray[] = $value;
            }   
    
        }
    
        return $newArray;
    
    }
    

    ...or pass a variable by reference like this:

    function recursive($array, &$newArray = null) {
    
        if (!$newArray) {
            $newArray = array();
        }
    
        foreach($array as $key => $value) {
    
            if(is_array($value)){
                recursive($value, $newArray);                              
            }else{
                $newArray[] = $value;
            }   
    
        }
    
        return $newArray;   
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)