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.