0

i have this array

$array =array('a', 'b', array(array('c'), 'd'), 'e', array('f'), 'g');

i want to have as result this

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
)

I am using this function :

function flatten ($array) {
    $result = array();
    array_walk_recursive($array, function($v, $k) use (&$result) {
        $result[] = $v;
    });
    return $result;
}

i am getting this result :

Array
(
    [0] => array('a', 'b', 

    [1] => array(array('c'), 'd'),

    [2] =>  'e', array('f'), 'g')
)

which the same as the original input array.

any help would be appreciated

Elias Van Ootegem
  • 70,983
  • 9
  • 108
  • 145
user888300
  • 93
  • 10

3 Answers3

2
function flatten($a) {
    if(!is_array($a))
        return array($a);
    return call_user_func_array(
        'array_merge',
        array_map('flatten', $a)
    );
}


$a = array('a', 'b', array(array('c'), 'd'), 'e', array('f'), 'g');
print_r(flatten($a));

gives the desired output.

http://sandbox.onlinephpfunctions.com/code/d5806a85b31b2bd84985f56916da2d5a5a9b6744

georg
  • 204,715
  • 48
  • 286
  • 369
  • @user888300: can you repost your original array using valid php syntax? (no var_dump). I suspect it's actually not what you think it is. – georg Jun 05 '14 at 12:40
  • 1
    @user888300: well, works for me. http://sandbox.onlinephpfunctions.com/code/d5806a85b31b2bd84985f56916da2d5a5a9b6744 – georg Jun 05 '14 at 12:54
1

Alternatively, you could also use SPL for this, particularly RecursiveIteratorIterator with RecursiveArrayIterator. Consider this example:

$array = array('a', 'b', array(array('c'), 'd'), 'e', array('f'), 'g');
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as $value) {
    $data[] = $value;
}
echo '<pre>';
print_r($data);
echo '</pre>';

Should yield same results:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
)
user1978142
  • 7,965
  • 3
  • 16
  • 20
  • COPY PASTE :) http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array – Dushyant Joshi Jun 05 '14 at 12:48
  • @DushyantJoshi really? you could look at all my answers i always recommend this to flatten arrays – user1978142 Jun 05 '14 at 12:51
  • Yes you are right. But you would have commented that link instead of posting same answer again and again. – Dushyant Joshi Jun 05 '14 at 12:52
  • 1
    here, http://stackoverflow.com/a/23996334/1978142, http://stackoverflow.com/a/23530520/1978142, http://stackoverflow.com/a/23966321/1978142 – user1978142 Jun 05 '14 at 12:53
  • @Dushyant Joshi: Can you link to the specific answer where this is copied from? Because I'm honestly not seeing it. – BoltClock Jun 06 '14 at 11:36
  • @BoltClock http://stackoverflow.com/a/1320259/1749007 here as I already pasted before. And the same guy has also provided the links where he answered after my comment in this answer. The difference is just a "variable" change which is to be sorted. The link can be given as comment also. – Dushyant Joshi Jun 06 '14 at 11:46
-1
function return_value($input) {
    $return = '';
    if (is_array($input)){
        foreach($input as $data) $return .= return_value($data);
    }
    else {
        $return .= $input.',';
    }
    return $return;
}

$array = Array ( 
    0 => array('a', 'b'), 
    1 => array(array('c'), 'd'), 
    2 => 'e', array('f'), 'g' 
);

$string = return_value($array);
$string = substr($string, 0, strlen($string)-1);

$new_array = explode(',', $string);
Gieerzetka
  • 106
  • 1
  • 8