PHP - get specific element form each sub array without looping -
in php there way element each sub array without having loop - thinking in terms of efficiency.
say following array:
$array = array( array( 'element1' => a, 'element2' => b ), array( 'element1' => c, 'element2' => d ) );
i of 'element1' values $array
there number of different functions can operate on arrays you, depending on output desired...
$array = array( array( 'element1' => 'a', 'element2' => 'b' ), array( 'element1' => 'c', 'element2' => 'd' ) ); // array of element1s : array('a', 'c') $element1a = array_map(function($item) { return $item['element1']; }, $array); // string of element1s : 'ac' $element1s = array_reduce($array, function($value, $item) { return $value . $item['element1']; }, ''); // echo element1s : echo 'ac' array_walk($array, function($item) { echo $item['element1']; }); // alter array : $array becomes array('a', 'c') array_walk($array, function(&$item) { $item = $item['element1']; });
useful documentation links: