0

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.

4 Answers 4

2
//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

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer , your solution works but the problem is that some elements are missing from the final array , any suggestion ?
I just tested wirh your example data, everything works there. Can you give the example data, that creates missing elements?
AHA! This has a different structure! E.g. inside Element 6 you have more than one child, but in your Question you allways have only 1 child ... suggest you update your question
you are right , thanks for spotting that , i updated my question
As you see, I updated my answer to work with the new structure
0

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.

Comments

0

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

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.