php - Implode on multiarray -
i have array:
$array = array( 0 => array('first' => 'aaa', 'second' => 'bbb'), 1 => array('first' => 'erw', 'second' => 'wer'), 2 => array('first' => 'aaawe', 'second' => '345'), 3 => array('first' => 'aa345a', 'second' => 'dfgdfg'), );
and values implode:
$first = implode(';', $array['first']); $second = implode(';', $array['second']);
but of course not working. how best way this?
with php 5.5 there exists function array_column()
: http://php.net/array_column
$first = implode(';', array_column($array, 'first')); // same $second
p.s.: backwards compability check https://github.com/ramsey/array_column out
as requested, "simple" (php 5.3 compatible):
$first = array_reduce($array, function ($array, $string) { return ($string !== null?"$string;":"").$array['first']; }, null);
Comments
Post a Comment