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

时间:2018-09-28 00:12:16

标签: php pass-by-reference recursive-datastructures

我只需要在回调中构建复杂的多级数组,每个调用一个原子更新。

这样做的原因:回调被迭代解析器多次调用。最后,它应该构建要解析的二进制格式的反序列化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']);
        $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']));

尝试here

上面的示例打印:

[1,2,[10,11]],但应该 [1,2,[10,11],3,4]

更新: 多级表示任意深度随机的数组。

更新: 问题出在array_pop()上,请参见下面的固定版本答案。

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作,这肯定会简化您要尝试执行的操作。它只是具有不同的基本数组值,但是足够相似,因此我认为它应该适合您的应用。

所做的更改:重组数组并使用addTo字符串作为'pointer'

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

// prepare container
$arr = [];
$arr['data'] = [];
$arr['addTo'] = 'data';
$arr['temp'] = [];

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

        case ACTION_LEAVE:
            $param['addTo'] = 'data';
            $param['data'][] = $param['temp'];
            $param['temp'] = [];
            break;

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



// 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']));

答案 1 :(得分:1)

我认为问题是array_pop,它没有返回对最后一个元素的实际引用。这个有效:

<?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']));