convert associative array to single array

Solutions on MaxInterview for convert associative array to single array by the best coders in the world

showing results for - "convert associative array to single array"
Erik
24 Oct 2018
1<?php 
2
3/**
4 * Convert a multi-dimensional array into a single-dimensional array.
5 * @author Sean Cannon, LitmusBox.com | seanc@litmusbox.com
6 * @param  array $array The multi-dimensional array.
7 * @return array
8 */
9function array_flatten($array) { 
10  if (!is_array($array)) { 
11    return false; 
12  } 
13  $result = array(); 
14  foreach ($array as $key => $value) { 
15    if (is_array($value)) { 
16      $result = array_merge($result, array_flatten($value)); 
17    } else { 
18      $result = array_merge($result, array($key => $value));
19    } 
20  } 
21  return $result; 
22}
similar questions
queries leading to this page
convert associative array to single array