Reputation: 913
Suppose we have an array with one attribute for each element:
Array ( [0] => A [1] => B [2] => C [3] => D )
Is it possible to add the same uniform attribute to each of the elements, so that a new two-dimensional array would be obtained? I.e. for attribute X:
Array ( [0] => Array ( [0] => A [1] => X)
[1] => Array ( [0] => B [1] => X)
[2] => Array ( [0] => C [1] => X) )
I know, I could do it using for each loop, but is there any more elegant solution (such as array_combine)?
(For assigning different values, please refer to: Assigning a value to each array element PHP)
Upvotes: 0
Views: 1949
Reputation: 9299
array_walk
could work as well.
$arr = array('A', 'B', 'C', 'D');
array_walk($arr, 'arrFunc');
function arrFunc(&$item, $key)
{
$item = array($item, 'X');
}
Upvotes: 1