3

I have this anonymous function $build_tree within another function that works fine in PHP 5.3

function nest_list($list) {
$index = array();
index_nodes($list, $index);

$build_tree = function(&$value, $key) use ($index, &$updated) {
    if(array_key_exists($key, $index)) {
        $value = $index[$key];
        $updated = true;
     todel($key); }
};

do {
    $updated = false;
    array_walk_recursive($list, $build_tree);
} while($updated);

return $list;
}

function index_nodes($nodes, &$index) {
    foreach($nodes as $key => $value) {
    if ($value) {
        $index[$key] = $value;
        index_nodes($value, $index);
                }
    }
}

How can I convert this into PHP 5.2 compatible code?

Justin
  • 305
  • 1
  • 7
  • 2
    http://stackoverflow.com/questions/5869084/php-5-3-0-use-keyword – malletjo Nov 23 '11 at 02:01
  • I think we'd also need the surrounding code that shows how this function is used to rewrite the algorithm properly. – deceze Nov 23 '11 at 02:02
  • I have updated the question with all surrounding code and related functions – Justin Nov 23 '11 at 02:13
  • It looks like your code is broken. `if(array_key_exists($key, $index))` <- here both the `$key` and `$index` appear to be the same array. @deceze is correct, you need to explain the algorithm you're trying to implement so that we can suggest a valid php 5.2 solution. – Josiah Nov 23 '11 at 02:21

2 Answers2

1

Generally, you could do this using an object's method (callbacks can be either a function, or an object's method; the latter allows you to maintain state). Something like this (not tested):

class BuildTree {
    public $index, $updated = false;
    public function __construct($index) {
        $this->index = $index;
    }
    function foo(&$value, $key) {
        if(array_key_exists($key, $this->index)) {
            $value = $this-.index[$key];
            $this->updated = true;
         todel($key); }
    }
}

do {
    $build_tree_obj = new BuildTree($index);
    array_walk_recursive($list, array($build_tree_obj, 'foo'));
} while($build_tree_obj->updated);

However, array_walk_recursive has a special feature that allows us to pass a third argument, which is a value that will be passed into every call of the function. Although the value is passed by value, we can cleverly use objects (reference types in PHP 5) to maintain state (from How to "flatten" a multi-dimensional array to simple one in PHP?):

$build_tree = create_function('&$value, $key, $obj', '
    if(array_key_exists($key, $index)) {
        $value = $index[$key];
        $updated = true;
     todel($key); }
');

do {
    $obj = (object)array('updated' => false);
    array_walk_recursive($list, $build_tree, $obj);
} while($obj->updated);
Community
  • 1
  • 1
newacct
  • 119,665
  • 29
  • 163
  • 224
0

I don't think this is possible without changing the way the function is called, because there is no mechanism in PHP 5.3 for a lambda function to change a variable from the scope it is called in (in this case $updated).

You could return $updated like this:

$build_tree = create_function('&$value,$key,$updated','
    $index = '.var_export($index).';
    if(array_key_exists($key, $index)) {
        $value = $index[$key];
        $updated = true;
     todel($key); }
     return $updated;
');

but then you have to call it like this:

$updated = $build_tree('the value','the key',$updated);
AndreKR
  • 32,613
  • 18
  • 106
  • 168