Get column from a two dimensional array

Taking a column is easy with the map function. // a two-dimensional array var two_d = [[1,2,3],[4,5,6],[7,8,9]]; // take the third column var col3 = two_d.map(function(value,index) { return value[2]; }); Why bother with the slice at all? Just filter the matrix to find the rows of interest. var interesting = two_d.filter(function(value,index) {return value[1]==5;}); // interesting … Read more

How to “flatten” a multi-dimensional array to simple one in PHP? [duplicate]

$array = your array $result = call_user_func_array(‘array_merge’, $array); echo “<pre>”; print_r($result); REF: http://php.net/manual/en/function.call-user-func-array.php Here is another solution (works with multi-dimensional array) : function array_flatten($array) { $return = array(); foreach ($array as $key => $value) { if (is_array($value)){ $return = array_merge($return, array_flatten($value));} else {$return[$key] = $value;} } return $return; } $array = Your array $result = … Read more

PHP combine two associative arrays into one array

array_merge() is more efficient but there are a couple of options: $array1 = array(“id1” => “value1”); $array2 = array(“id2” => “value2”, “id3” => “value3”, “id4” => “value4”); $array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/); $array4 = $array1 + $array2; echo ‘<pre>’; var_dump($array3); var_dump($array4); echo ‘</pre>’; // Results: array(4) { [“id1”]=> string(6) “value1” [“id2”]=> string(6) “value2” [“id3”]=> … Read more