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 5 years have passed since last update.

PHP の array_reduce 関数 分かりやいサンプル

Posted at

array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) : mixed

array_reduce() は、配列 array の各要素に callback 関数を繰り返し適用し、 配列を一つの値に減らします。

例:



<?php

function sum($carry, $item) {
    var_dump($carry, $item);
    $carry += $item;
    echo '<br><hr>';
    return $carry;
}

$a = array(1, 2, 3, 4, 5);

var_dump(array_reduce($a, 'sum', 10));

結果

int(10) int(1)
int(11) int(2)
int(13) int(3)
int(16) int(4)
int(20) int(5)
int(25)

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?