yano3nora
10/3/2017 - 2:44 PM

[php: array_walk] Destructive array scan with closures. #php

[php: array_walk] Destructive array scan with closures. #php

<?php 

$adding  = 120;

$path    = '.1.2.3.4.';
$trimmed = trim($path, '.');
$paths   = explode('.', $trimmed);

// Important: Should be passing reference!
array_walk($paths, function(&$item) use ($adding) {
  $item += $adding;
});

$newPath = '.'.implode('.', $paths).'.';

var_export($newPath);  // .121.122.123.124.