简体   繁体   中英

array_walk only leaving values?

I have this code:

array_walk(
    array('foo' => 2, 'bar' => 5, ...),
    function ($v, $k) { return $k . '=' . $v; }
);

But after this, all thats left is array(2, 5) .

Why is this the case and how do I get the expected result of array('foo=2', 'bar=5') ?

Pass your value by reference, like

$rgData = array('foo' => 2, 'bar' => 5);
array_walk(
    $rgData,
    function (&$v, $k) { $v = $k . '=' . $v; }
);

Also note that referenced value itself should be changed (return is not necessary here, only $v changing matters)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM