Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have an array like the following:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 3
        )

    [2] => Array
        (
            [0] => 4
        )

)

Now I want each array value into one single array. How do I do it?

Thanks in advance.

share|improve this question

marked as duplicate by John Conde, webbiedave, aynber, Pitchinnate, andrewsi Oct 5 '13 at 1:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
So you want array(1,2,3,3,4) ? –  Pitchinnate Oct 4 '13 at 20:19

2 Answers 2

You can recursively parse the array with a function:

$multiDimArr = array(...);

function getSingleArray( $multiDimArr ) {

   $singleArray = array();

   foreach($multiDimArr as $row) {
       if( is_array($row) ) {
           getSingleArray($row); // recursive call -> row it cand be also multi dimensional
       }
       else {
           $singleArray[] = $val;
       }
   }

   return $singleArray;
}

I really hope this will help!!

share|improve this answer
// PHP >= 5.3:
function array_value_recursive($key, array $arr){
    $val = array();
    array_walk_recursive($arr, function($v, $k) use($key, &$val){
        if($k == $key) array_push($val, $v);
    });
    return $val;
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.