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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:02:45+00:00 2026-06-06T11:02:45+00:00

All, I have the following array and function, which will create a multidimensional array

  • 0

All,

I have the following array and function, which will create a multidimensional array based on array key’s that you specific. For every attribute you pass to the function, it will add another dimension to the array. Think of it as array sorting.

The function supplied works great, but it uses eval, I had a hard time coming up with a function which was consistent and threw no errors without it.

Let’s start with an array:

$array = array(
          array(‘name’ => ‘Person1’, ‘username’ => ‘username1’, ‘join_date’ => 12233445566, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person2’, ‘username’ => ‘username2’, ‘join_date’ => 12233445566, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person3’, ‘username’ => ‘username3’, ‘join_date’ => 12233445996, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person4’, ‘username’ => ‘username4’, ‘join_date’ => 12233445996, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person5’, ‘username’ => ‘username5’, ‘join_date’ => 12233445566, ‘state’ => ‘NJ’),
          array(‘name’ => ‘Person6’, ‘username’ => ‘username6’, ‘join_date’ => 12233445566, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person7’, ‘username’ => ‘username7’, ‘join_date’ => 12233445776, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person8’, ‘username’ => ‘username8’, ‘join_date’ => 12233445566, ‘state’ => ‘NY’),
          array(‘name’ => ‘Person9’, ‘username’ => ‘username9’, ‘join_date’ => 12233445996, ‘state’ => ‘NJ’),
);

Here is an example function:

function createIndex($array, $index){
   $index_array = array();

   foreach($array as $result){

          if(is_array($index)){
                 $key = '$index_array';
                 for($i=0;$i<=sizeof($index)-1;$i++){
                       $key .= "['{$result[$index[$i]]}']";
                 }
                 $key .= "[]"; 
                 eval("$key = \$result;");
          }
          else{
                 $index_array[$result[$index]] = $result; 
          }
   }

   return $index_array;
}

The Calling function:

print_r(create_index($array, array(‘state’, ‘join_date’)));

The desired output:

Array
(
[NJ] => Array
    (
        [12233445566] => Array
            (
                [0] => Array
                    (
                        [name] => Person1
                        [username] => username1
                        [join_date] => 12233445566
                        [state] => NJ
                    )

                [1] => Array
                    (
                        [name] => Person2
                        [username] => username2
                        [join_date] => 12233445566
                        [state] => NJ
                    )

                [2] => Array
                    (
                        [name] => Person5
                        [username] => username5
                        [join_date] => 12233445566
                        [state] => NJ
                    )

            )

        [12233445996] => Array
            (
                [0] => Array
                    (
                        [name] => Person4
                        [username] => username4
                        [join_date] => 12233445996
                        [state] => NJ
                    )

                [1] => Array
                    (
                        [name] => Person9
                        [username] => username9
                        [join_date] => 12233445996
                        [state] => NJ
                    )

            )

    )

[NY] => Array
    (
        [12233445996] => Array
            (
                [0] => Array
                    (
                        [name] => Person3
                        [username] => username3
                        [join_date] => 12233445996
                        [state] => NY
                    )

            )

        [12233445566] => Array
            (
                [0] => Array
                    (
                        [name] => Person6
                        [username] => username6
                        [join_date] => 12233445566
                        [state] => NY
                    )

                [1] => Array
                    (
                        [name] => Person8
                        [username] => username8
                        [join_date] => 12233445566
                        [state] => NY
                    )

            )

        [12233445776] => Array
            (
                [0] => Array
                    (
                        [name] => Person7
                        [username] => username7
                        [join_date] => 12233445776
                        [state] => NY
                    )

            )

    )

)

The question: What are ways that you would conquer the above to obtain the same results form the same array? I am curious to see how others would do it.

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-06-06T11:02:46+00:00Added an answer on June 6, 2026 at 11:02 am

    Using references, I was able to come up with the below function. It works great!

    It will automatically detect if multiple leafs exist at the end of the tree, and if so, branch, or leaf itself. Also, you can merge, using the $merge_classifier argument. This will allow you to merge columns called count for example, which will add the values together.

    function createIndex($keys, Array $array, $merge_classifier = NULL, $return_array = array())
    {               
    
        if(is_string($keys)){
            $tmp = $keys;
            $keys = array();
            $keys[] = $tmp; 
        }
    
        foreach($array as $result) {
            $object = &$return_array;
    
            foreach($keys as $index){
                if(!array_key_exists($result[$index], $object)) {
                    $object[$result[$index]] = array();
                }
                $object = &$object[$result[$index]];
            }
    
            if(!is_null($merge_classifier)){
                $object = array_merge($result, array($merge_classifier => ($result[$merge_classifier] + $object[$merge_classifier])));
            }
            else{
                if(sizeof($object) > 0){
                    $object[] = $result;    
                }else{
                    $object = $result;  
                }
            }
        }
    
        return $return_array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP function which will list the users from a MySql
I have the following code: function build_all_combinations(input_array){ array = [1,2,3] limit = array.length -
All, I have the following code: $context = stream_context_create(array( 'http' => array( 'timeout' =>
I have following script that executes all the .reg files in the current directory.
I have the following query to count all data every minute. $sql= SELECT COUNT(*)
I have written the following function which adds an event listener and registers the
I have to create a function which gets 5 parameters representing information about the
I have a function which returns only one row as array. I give a
I have the following code, which loops through an array of menuoptions and on
All, I'm trying to use the do_shortcode function. I have the following jQuery to

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.