Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:28:17+00:00 2026-05-29T17:28:17+00:00

Suppose i have a array like this : Array( ‘1’ => Array( ID =>

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:28:35+00:00Added an answer on May 29, 2026 at 5:28 pm
    //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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an array like this: $products = array('Shoes' => array('price' => 49.99,
Suppose you have output like this: Word1 Word2 Word3 Word4 Where the number of
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
Suppose I have something like this int strLen; printf(Please enter a number: ); scanf(%d,
Let's suppose I have n arrays, where n is a variable (some number greater
I have an array with a series of event IDs organized like this: $event['year']['month']['day']
Suppose I have an element like this: <div id=dv class=red green blue>Something Here !!</div>
Suppose I have a regex like this: @\b(one|two|three)\b; How can I get all of
Suppose we have a code block like this : //Assuming s is a stream:
Hi I am using Mathematica 5.2. Suppose I have an array list like In[2]:=lst=Tuples[{0,1},4]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.