Mouna Cheikhna
Mouna Cheikhna

Reputation: 39628

Put nested array into one array

Suppose i have a array like this :

 Array(
      '1' => Array(
                 "ID" => 1,
                  "Name" => "name 1"
            ),
      '2' => Array (
                 Array(
                   "ID" => 2,
                   "Name" => "name 2"
                 )
             ),
      '3' => Array(
                 Array(
                   Array(
                     Array(
                        "ID" => 3,
                         "Name" => "name3"
                     )
                 )
              ),
       '4' => Array (
                 Array {
                  "ID" => 4,
                  "Name" => "name 4"
                 ),
                 Array(
                  "ID" => 5,
                  "Name" => "name 5"
                ),
                Array(
                  "ID" => 6,
                  "Name" => "name 6"
                )
             );

number of sub-arrays is not ordered it may be 3, 4 or 5 etc... and i wanted to get :

Array(
    Array( "ID" => 1, "Name" => "name 1"),
    Array( "ID" => 2, "Name" => "name 2"),
    Array( "ID" => 3, "Name" => "name 3"),
    Array( "ID" => 4, "Name" => "name 4"),
    Array( "ID" => 5, "Name" => "name 5"),
    Array( "ID" => 6, "Name" => "name 6"));

Is there an easy way to do this ?

EDIT :

Edited the above array to add :

'4' => Array (
                 Array {
                  "ID" => 4,
                  "Name" => "name 4"
                 ),
                 Array(
                  "ID" => 5,
                  "Name" => "name 5"
                ),
                Array(
                  "ID" => 6,
                  "Name" => "name 6"
                )
             );

which aren't nested but i still want them to be in my final array.

Thanks.

Upvotes: 0

Views: 100

Answers (4)

Eugen Rieck
Eugen Rieck

Reputation: 65284

//Assuming your data is in $in

$out=array();
foreach($in as $k=>$v) {
  while ((is_array($v)) && (isset($v[0]))) $v=$v[0];
  //See below for next line
  $out[]=$v;
}

print_r($out);

With the marked line being either $out[$k]=$v; or $out[]=$v; depending on wether you want to keep the 1st-level keys. In your desired output you do not ,so use the shown version

Edit

With you changed input array, you need to do something like

function addtoarray($inarray, &$outarray) {
  foreach ($inarray as $i) {
    if (!is_array($i)) continue;
    if (isset($i['ID'])) $outarray[]=$i;
    else addtoarray($i,$outarray);
  }
}

$out=array();
addtoarray($in,$out);
print_r($out);

This was tested against your new input data

Upvotes: 2

Casey Kinsey
Casey Kinsey

Reputation: 1441

Wrote up a quick recursive function. You'll need to write any appropriate error handling and check the logic, since a discrepancy in your data could easily turn this into an infinite loop:

$output = array();

foreach ( $data as $d ) {
    $output[] = loop_data( $d );
}


function loop_data( $data ) {
    // Check if this is the right array
    if( ! array_key_exists( "ID", $data ) ) {
        // Go deeper
        $data = loop_data( $data[0] );
    }

    return $data;
}

Upvotes: 0

Jason Brumwell
Jason Brumwell

Reputation: 3550

This may work for you, assuming $array is the variable and php 5.3

//process
$array = array_map(function($block) {
    $return = '';

    foreach ($block as $sub) {
        if (true === isset($sub['ID']) {
            $return = $block;
            break;
        }
    }

    return $block;
}, $array);

array_filter($array);  //remove empty elements

Upvotes: 0

kitti
kitti

Reputation: 14824

You could recurse through the array and check for the ID field.

function combine_array(array $src, array &$dest)
{
    if( isset( $src['ID'] ) ) {
        $dest[] = $src;
        return;
    }

    foreach( $sub in $src ) {
        combine_array( $sub, $dest );
    }
}

Just wrote this off the top of my head, no testing, so it might have a few problems. But that's the basic idea.

Upvotes: 0

Related Questions