duanhuang7591 2018-09-28 00:12
浏览 83
已采纳

PHP 7 - 在回调中迭代填充多级数组

I need to build complex multilevel array in callback only, one atomic update per call.

The reason for it: callback is called many times from iterative parser. Finally it should build deserialized PHP-array of binary format being parsed.

Here is runnable code:

const ACTION_VALUE = 1;
const ACTION_ENTER = 2;
const ACTION_LEAVE = 3;

function callback($action, $value, &$param)
{
   switch ($action) 
   {
    case ACTION_ENTER:
        $param['parent'][] = &$param['current'];
        $param['current'][] = [];

        end($param['current']);                
        $param['current'] = &$param['current'][key($param['current'])];
    break;

    case ACTION_LEAVE:
        unset($param['current']);
        $param['current'] = array_pop($param['parent']);
        end($param['current']);
    break;

    case ACTION_VALUE:
        $param['current'][] = $value;
    break;
   }
}

// prepare container
$arr = [];
$arr['data'] = [];
$arr['current'] = &$arr['data'];
$arr['parent'] = [];

// callback invocations
callback(ACTION_VALUE, 1, $arr);
callback(ACTION_VALUE, 2, $arr);
callback(ACTION_ENTER, 0, $arr);
callback(ACTION_VALUE, 10, $arr);
callback(ACTION_VALUE, 11, $arr);
callback(ACTION_LEAVE, 0, $arr);
callback(ACTION_VALUE, 3, $arr);
callback(ACTION_VALUE, 4, $arr);

// now see result
var_dump(json_encode($arr['data']));

Try it here

Above example prints:

[1,2,[10,11]], but should [1,2,[10,11],3,4].

Update: Multilevel means arbitrary arrays of random depth.

Update: The problem was with array_pop(), see below accepted answer for fixed version.

  • 写回答

2条回答 默认 最新

  • dongpu1315 2018-09-28 10:49
    关注

    I think the problem is array_pop which doesn't return actual reference to the last element. This one works:

    <?php
    
    const ACTION_VALUE = 1;
    const ACTION_ENTER = 2;
    const ACTION_LEAVE = 3;
    
    function callback($action, $value, &$param)
    {
        switch ($action) {
            case ACTION_ENTER:
                $param['parent'][] = &$param['current'];
                $param['current'][] = [];
    
                end($param['current']);
                $param['current'] = &$param['current'][key($param['current'])];
                break;
    
            case ACTION_LEAVE:
                unset($param['current']);
                end($param['parent']);
                $param['current'] = &$param['parent'][key($param['parent'])];
                unset($param['parent'][key($param['parent'])]);
                end($param['current']);
                break;
    
            case ACTION_VALUE:
                $param['current'][] = $value;
                break;
        }
    }
    
    // prepare container
    $arr = [];
    $arr['data'] = [];
    $arr['current'] = &$arr['data'];
    $arr['parent'] = [];
    
    // callback invocations
    callback(ACTION_VALUE, 1, $arr);
    callback(ACTION_VALUE, 2, $arr);
    callback(ACTION_ENTER, 0, $arr);
    callback(ACTION_VALUE, 10, $arr);
    callback(ACTION_VALUE, 11, $arr);
    callback(ACTION_ENTER, 0, $arr);
    callback(ACTION_VALUE, 40, $arr);
    callback(ACTION_VALUE, 41, $arr);
    callback(ACTION_LEAVE, 0, $arr);
    callback(ACTION_LEAVE, 0, $arr);
    callback(ACTION_VALUE, 3, $arr);
    callback(ACTION_VALUE, 4, $arr);
    
    // now see result
    var_dump(json_encode($arr['data']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗