0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

phpのarray_walkのcallbackで参照渡しの変数を使う

Posted at

array_walkのcallbackのargに参照渡しができない。
以下のようにclosureを参照渡ししたらうまくいった。

<?php

$a = [ 'a'=>1,'b'=>-2,'c'=>3 ] ;

$sum=0;
array_walk($a, function($v,$k) use (&$sum){ $sum += abs($v); echo "{$k}:{$sum} ";});
echo "\n{$sum}\n";

実行結果

$ php aw.php
a:1 b:3 c:6 
6
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?