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 512483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:19:28+00:00 2026-05-13T07:19:28+00:00

I am trying to create a single array that contains all of the values

  • 0

I am trying to create a single array that contains all of the values of an existing multidimensional array. This is what I have so far:

function MaxArray($arr) {

    foreach ($arr as $value) {

        if (is_array($value)) {
            MaxArray($value);
        } else {
            $array[] = $value;
        }
    }
    print_r($array);

}

$arr = array(array(141,151,161), 2, 3, array(101, 202, array(303,404, array(1,2))));

MaxArray($arr);

When I execute this code, I get this response from the print_r function…

Array ( [0] => 141 [1] => 151 [2] => 161 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 303 [1] => 404 ) Array ( [0] => 101 [1] => 202 ) Array ( [0] => 2 [1] => 3 ) 

As you can see, this is not what I am looking for and I can’t figure out how to combine all of the values in the multidimensional array into a single array. Can anyone please point me in the right direction here?

  • 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-13T07:19:28+00:00Added an answer on May 13, 2026 at 7:19 am

    What you’re trying to do is often called ‘array flattening’, so ArrayFlatten is probably a better name for a function than MaxArray (since the latter sounds like it’ll return the highest value in the array, which max does very well). ArrayFlatten could be written like this:

    function ArrayFlatten($array, $return) {
      for ($x = 0; $x < count($array); $x++) {
        if(is_array($array[$x])) {
          $return = ArrayFlatten($array[$x], $return);
        }
        else {
          if($array[$x]) {
            $return[] = $array[$x];
          }
        }
      }
    
      return $return;
    }
    

    And used like this:

    $myarray = array('a','b',array(array(array('x'),'y','z')),array(array('p')));    
    $res = ArrayFlatten($myarray,array());
    

    To get this:

    Array
    (
        [0] => a
        [1] => b
        [2] => x
        [3] => y
        [4] => z
        [5] => p
    )
    

    From here.

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

Sidebar

Related Questions

I want to create an array in numpy that contains the values of a
I am trying to create a array that should be something like this: [
I have a single xml parsing function that I'm trying to call multiple times
I am trying to create buttons that play single sound files and one button
I'm trying to create a search where a single record must have multiple records
I'm trying to create a single, one-off page that will combine information from several
Im trying to create a function, that will return a mysql query, which i
I am trying to create a form that takes only a single letter and
I am trying to create a table with a single column using the following
I am new to objective-c and I am trying to create an array of

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.