Menu

Saturday, November 13, 2021

array_map() push new array into previous array and making new indexes


array_map() function modify all elements one or more arrays according to some user-defined condition. The array_map() is an inbuilt function in PHP and it works with PHP array. To apply a function to every item in an array, use array_map(). This will return a new array.


Syntax:

    array_map (function name, array1,array2...);


Use array_map to add the string to each of the variables,


<?php

$x = 'x string';
$y = 'y string';
$z = true;
$z ? array_map(function (&$v) { $v .= " z string";}, [&$x, &$y]) : false;

echo "$x\n$y\n";

?>