FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » array_walk_recursive and call by reference - possible?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
array_walk_recursive and call by reference - possible? [message #176922] Sun, 05 February 2012 18:07 Go to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Hi,

I have designed an array with occasional arrays as values, to mimic the structure of
a html select element with optgroup entries.

Now I need some functions like counting all selectable elements, and searching a key.

I thought array_walk_recursive() was appropriate for this task. But now I have a
problem getting values out of my callback. I could use globals here, but nicer seemed
to be: an extra function parameter "by reference".

Now I keep getting PHP Deprecated: Call-time pass-by-reference has been deprecated
in ...

I know I can suppress these messages, but is there an "official" way to pass
variables by reference with a callback?

I tested all possible combinations, test script attached.

/Str.

<?php
/**
* Testen von array_walk
* result: define by ref does not matter, call by ref is needed
*/

error_reporting(-1);

$sub1 = array('I'=>'Igor', 'J'=>'Jan', 'K'=>'Karl', 'L'=>'Liugi', 'M'=>'Michael');
$sub2 = array('A'=>'Alexa', 'B'=>'Brunhilda','C'=>'Carla','D'=>'Dina','E'=>'Elsa');
$a['F'] = 'Frantzi';
$a['G'] = 'Gustavo';
$a['Gentlemen'] = $sub1;
$a['H'] = 'Hillbilly';
$a['Ladies'] = $sub2;

function findindex($v,$k,$var) {
if ($k=='K') $var = $v;
}
function findindex1($v,$k, &$var) {
if ($k=='K') $var = $v;
}

echo '------------------------------', PHP_EOL;
echo "\tStart run\n";
echo '------------------------------', PHP_EOL;
#
# test call by ref
#
$var = 'something';
echo "var before array_walk: ($var)\n";
# this does work, var will be defined: unset($var);
$rc = array_walk_recursive($a, 'findindex', &$var);

echo "rc = $rc, var after array_walk with call by ref: ($var)\n";
echo '------------------------------', PHP_EOL;
#
# test define by ref
#
$var = 'something';
echo "var before array_walk: ($var)\n";
$rc = array_walk_recursive($a, 'findindex1', $var);

echo "rc = $rc, var after array_walk with define by ref, call by val: ($var)\n";
echo '------------------------------', PHP_EOL;
#
# test define by ref, call by ref
#
$var = 'something';
echo "var before array_walk: ($var)\n";
$rc = array_walk_recursive($a, 'findindex1', &$var);

echo "rc = $rc, var after array_walk with define by ref + call by ref: ($var)\n";
echo '------------------------------', PHP_EOL;
Re: array_walk_recursive and call by reference - possible? [message #176926 is a reply to message #176922] Sun, 05 February 2012 21:31 Go to previous message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 05.02.2012 19:07, schrieb M. Strobel:
> Hi,
>
> I have designed an array with occasional arrays as values, to mimic the structure of
> a html select element with optgroup entries.
>
> Now I need some functions like counting all selectable elements, and searching a key.
>
> I thought array_walk_recursive() was appropriate for this task. But now I have a
> problem getting values out of my callback. I could use globals here, but nicer seemed
> to be: an extra function parameter "by reference".
>
> Now I keep getting PHP Deprecated: Call-time pass-by-reference has been deprecated
> in ...
>
> I know I can suppress these messages, but is there an "official" way to pass
> variables by reference with a callback?
>
> I tested all possible combinations, test script attached.
>
> /Str.

script cut off

Maybe I should have shown the result of the test:

strobel@s114-intel:~/projekte/PHP> php test-array-walks.php
PHP Deprecated: Call-time pass-by-reference has been deprecated in
/home/strobel/projekte/PHP/test-array-walks.php on line 38

Deprecated: Call-time pass-by-reference has been deprecated in
/home/strobel/projekte/PHP/test-array-walks.php on line 38
PHP Deprecated: Call-time pass-by-reference has been deprecated in
/home/strobel/projekte/PHP/test-array-walks.php on line 56

Deprecated: Call-time pass-by-reference has been deprecated in
/home/strobel/projekte/PHP/test-array-walks.php on line 56
Number of elements using count(): 5
array_key_exists() does NOT work in sub-arrays
------------------------------
Start run
------------------------------
var before array_walk: (something)
rc = 1, var after array_walk with call by ref: (Karl)
------------------------------
var before array_walk: (something)
rc = 1, var after array_walk with define by ref, call by val: (something)
------------------------------
var before array_walk: (something)
rc = 1, var after array_walk with define by ref + call by ref: (Karl)
------------------------------

Résumé: every working combination of by-ref/by-val gets a deprecated message.

/Str.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Tips for choosing a good e-commerce web development firm
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 07:04:22 GMT 2024

Total time taken to generate the page: 0.02246 seconds