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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:57:51+00:00 2026-05-25T22:57:51+00:00

I have some functions that deal with data provided in array format. These functions

  • 0

I have some functions that deal with data provided in array format. These functions all do the same job, so I would like to merge them into a single function.

The problem is, each of them receives arrays with different depths: one-, two- and three-dimensional arrays are used and, in some future implementations, even four-dimensional arrays may be used.

In any case, the significant and necessary data are always in the two innermost arrays, so I need to get rid of the outer arrays until I have only the innermost two-levels.
My doubt is not simply how to do it, but how to do it elegantly and efficiently, as I find my current method rather clumsy.

Current method:

function add() {
    $data = func_get_args();
    if(count($data)>0) {
        if(is_array($data[0])) {
            if(is_array($data[0][0])) {
                foreach($data[0] as $row) {
                    $this->items[] = $row;
                }
            } else {
                $this->items[] = $data[0];
            }
        } else {
            $this->items[] = $data;
        }
    }
} 

Some use examples:

$list->add('one', 'two', 'three', 'four', 'five');

$list->add($data_from_DB_in_array_format);

$list->add(
    array(
        array('one', 'two', 'three', 'four', 'five'),
        array('six', 'seven', 'eight', 'nine', 'ten')
        )
    );

$list->add(
    array(
        array(
            array('one', 'two', 'three', 'four', 'five'),
            array('six', 'seven', 'eight', 'nine', 'ten')
            )
        )
    );

As the data is recovered via func_get_args(), everything is put inside an extra array.

The result must be something like this:

    array(
        array('one', 'two', 'three', 'four', 'five'),
        array('six', 'seven', 'eight', 'nine', 'ten')
        );
  • 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-25T22:57:52+00:00Added an answer on May 25, 2026 at 10:57 pm

    There is only one way you can achieve this, and it is with recursiveness and a little array function magic. It is probably not your exact solution but should get you started, if you need more help buzz me:

    <?php
    
    $data = array(
        'test1' => array(
            'test2' => array(
                'test3' => array(
                    'test4' => array(
                        1,
                        2,
                        3
                    ),
                    'test5' => array(
                        4,
                        5,
                        6
                    ),
                    'test6' => array(
                        7,
                        8,
                        9
                    ),
                )
            )
        )
    
    );
    function returnLast2Levels($item){
       if(is_array($item) && is_array(reset($item)) && is_array(reset(reset($item)))){
          //This $item has more than 2 levels, delve deeper
          return returnLast2Levels(reset($item));
       }elseif(is_array($item) && is_array(reset($item)) && !is_array(reset(reset($item)))){
          //This $item has 2 levels deep of array
          return $item;
       }
    }
    
    var_dump(returnLast2Levels($data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Deal all, I have implemented some functions and like to ask some basic thing
I have some code in a couple of different functions that looks something like
I'd like to write a function that would have some optional code to be
I have some global variables in a Python script. Some functions in that script
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have some setup code in my functions.php file that sets permalinks and adds
I'm developing a XPCOM C++ component that have to use some RegExps functions Does
I have a function that loads in some XML that looks like this: private
So I have some function that receives N random 2D points. Is there any
Does .NET have some type of built in function that allows filtering on Gridviews?

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.