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

  • Home
  • SEARCH
  • 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 3594864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:46:28+00:00 2026-05-18T19:46:28+00:00

Given a multi-dimensional array, I’m looking for a method that will extract various parts

  • 0

Given a multi-dimensional array, I’m looking for a method that will extract various parts of that array, given variable (i.e., different) criteria.

For example, if this is my data:

array(
  '0' => array(
        '0' => 'aaaaaa',
        '1' => 'bbbbb',
        '2' => 'ccccc'
  ), 
  '1' => array(
        '0' => 'aa2ssa',
        '1' => 'bb3242bb,
        '2' => 'ccccc234'
  ),
  '2' => array(
        '0' => 'aaa234aa',
        '1' => 'b3242b',
        '2' => 'cewrcc'
  ),
      (etc)
)

I want to be able to call a function

function new_array( index, sub_index )      

that returns an array based upon the index and sub_index parameters. Using the same data but different parameters would return different data.

Example 1

new_array( array(0, 2), ( array(1, 2), array(0, 2) ) )

Expected results:

array(
  '0' => array(
        '1' => 'bbbbb',
        '2' => 'ccccc'
  ), 
  '2' => array(
        '0' => 'aaa234aa',
        '2' => 'cewrcc'
  )
)

Example 2

new_array( array(2), ( array(0, 2) ) )

Expected results:

array(
  '2' => array(
        '0' =>'aaa234aa',
        '1' => 'b3242b'
  )
)

Anybody know how to do this? Thank you!

  • 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-18T19:46:28+00:00Added an answer on May 18, 2026 at 7:46 pm

    An alternate solution to @Orbling’s is this:

    function populateData() // CHANGE ME to populate the info how you please
    {
      $seed = 'abcdefghijklmnopqrstuvwxyz0123456789';
      $_ = ''; $length = rand(5,8);
      for ($__ = 0; $__ < $length; $__++) $_ .= substr($seed,rand(0,strlen($seed)),1);
      return $_;
    }
    
    function new_array()
    {
      $args = func_num_args();
      if ($args == 0)
        return FALSE; // flag error if no arguments are passed
    
      $template = func_get_arg(0);
      if ($template == null || !is_array($template) || $args < (count($template)+1))
        return FALSE; // flag error if we don't have enough information
    
      $resultAry = Array();
      $arg = 1;
      foreach ($template as $t)
      {
        $resultArySub = Array();
    
        $templateSub = func_get_arg($arg++);
        if ($templateSub == FALSE || !is_array($templateSub)) 
          return FALSE; // error checking for valid input
    
        foreach ($templateSub as $tS)
          $resultArySub[$tS] = populateData();
    
        $resultAry[$t] = $resultArySub;
      }
      return $resultAry;
    }
    
    header('Content-Type: text/plain');
    echo "your request (or so i understood):\r\n";
    $test = new_array(array(0,2),array(1,2),array(0,2));
    var_dump($test);
    
    echo "\r\nextra array on end is ignored:\r\n";
    $test = new_array(array(4,2),array(1,2),array(0,2),array(3,5));
    var_dump($test);
    
    echo "\r\nno data is a FALSE:\r\n";
    $test = new_array();
    var_dump($test);
    
    echo "\r\ntoo few arguments for what was supplied in first argument is a FALSE:\r\n";
    $test = new_array(array(1,2,3),array(4,5),array(6,7));
    var_dump($test);
    
    echo "\r\nas long as there's as \"array argument\" for every element of the \"first argument\", this will work:\r\n";
    $test = new_array(array(1,2,3,4,5,6,7),array(1),array(2),array(3),array(4),array(5),array(6),array(7));
    var_dump($test);
    
    echo "\r\nall arguments must be an array\r\n";
    $test = new_array(array(1,2),'not','arrays');
    var_dump($test);
    

    Results in an array with random entries. The outcome of the above would be:

    your request (or so i understood):
    array(2) {
      [0]=>
      array(2) {
        [1]=>
        string(8) "mjdfsmda"
        [2]=>
        string(8) "qg2bzsj6"
      }
      [2]=>
      array(2) {
        [0]=>
        string(7) "345plm8"
        [2]=>
        string(7) "1exlla6"
      }
    }
    
    extra array on end is ignored:
    array(2) {
      [4]=>
      array(2) {
        [1]=>
        string(5) "0ngei"
        [2]=>
        string(5) "q6tmg"
      }
      [2]=>
      array(2) {
        [0]=>
        string(7) "4enz61q"
        [2]=>
        string(6) "6bojtn"
      }
    }
    
    no data is a FALSE:
    bool(false)
    
    too few arguments for what was supplied in first argument is a FALSE:
    bool(false)
    
    as long as there's as "array argument" for every element of the "first argument", this will work:
    array(7) {
      [1]=>
      array(1) {
        [1]=>
        string(7) "ndulmi9"
      }
      [2]=>
      array(1) {
        [2]=>
        string(7) "jip402j"
      }
      [3]=>
      array(1) {
        [3]=>
        string(5) "3bn0d"
      }
      [4]=>
      array(1) {
        [4]=>
        string(8) "b80le1jh"
      }
      [5]=>
      array(1) {
        [5]=>
        string(5) "x31sw"
      }
      [6]=>
      array(1) {
        [6]=>
        string(8) "x8e3dge7"
      }
      [7]=>
      array(1) {
        [7]=>
        string(8) "vcpf997y"
      }
    }
    
    all arguments must be an array
    bool(false)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to solve the following question which i can't get to work by
I want the messagebox to only show if the number is equal to 0.
I have several USB mass storage flash drives connected to a Ubuntu Linux computer

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.