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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:31:48+00:00 2026-06-05T13:31:48+00:00

I want to combine the three arrays shown below and I don’t know how

  • 0

I want to combine the three arrays shown below and I don’t know how to do that, I want to take the companyCode of each array and make it for one array.

I have an array like this,

Array(
   [0] => Array(
             [companyCode] => SimpleXMLElement Object(
                            [0] => 'AD'
                     )
             [name] => SimpleXMLElement Object(
                            [0] => 'NYCT01'
                     )
             [vehicleRentalPrefType] => SimpleXMLElement Object(
                            [0] => 'ECAR'
                     )
             [rateAmount] => SimpleXMLElement Object(
                            [0] => '295.62'
                     )
         );
   [1] => Array(
             [companyCode] => SimpleXMLElement Object(
                            [0] => 'AD'
                     )
             [name] => SimpleXMLElement Object(
                            [0] => 'NYCT01'
                     )
             [vehicleRentalPrefType] => SimpleXMLElement Object(
                            [0] => 'SCAR'
                     )
             [rateAmount] => SimpleXMLElement Object(
                            [0] => '356.25'
                     )
   [2] => Array(
             [companyCode] => SimpleXMLElement Object(
                            [0] => 'AD'
                     )
             [name] => SimpleXMLElement Object(
                            [0] => 'NYCT01'
                     )
             [vehicleRentalPrefType] => SimpleXMLElement Object(
                            [0] => 'PCAR'
                     )
             [rateAmount] => SimpleXMLElement Object(
                            [0] => '562.36'
                     )
          )
)

I want this:

Array(
    [AD] => Array(
              [NYCT01] => Array(
                        [ECAR] => Array(
                                  [0] => '295.62'
                          )
                        [SCAR] => Array(
                                  [0] => '356.25'
                          )
                        [PCAR] => Array(
                                  [0] => '562.36'
                          )
                 )
        )
)

How can I do that, I tried to do some loops but I’m getting no result please help.

  • 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-05T13:31:49+00:00Added an answer on June 5, 2026 at 1:31 pm

    Here’s a function I created to group arrays a couple of days ago:

    for PHP 5.3 and above

    class Fx
    {
        public static function GroupBy(&$arr, $groupby, $valueFunctions = null)
        {
            $groupby = is_array($groupby) ? $groupby : array($groupby);
            $valueFunctions = is_array($valueFunctions) ? $valueFunctions : array($valueFunctions);
    
            $group1 = array();
            foreach($arr as $elem)
            {
                $group = is_callable($groupby[0]) ? $groupby[0]($elem) : $elem[$groupby[0]];
                $group1[$group][] = is_callable($valueFunctions[0]) ? $valueFunctions[0]($elem) : (is_null($valueFunctions[0]) ? $elem : $elem[$valueFunctions[0]]);
            }
    
            if(count($groupby) > 1)
            {
                $group2 = array();
                $gb_next = array_slice($groupby, 1);
                $vf_next = (count($valueFunctions) > 1) ? array_slice($valueFunctions, 1) : null;
                foreach($group1 as $group => $elems)
                {
                    $group2[$group] = self::GroupBy($elems, $gb_next, $vf_next);
                }
    
                return $group2;
            }
    
            return $group1;
        }
    }
    

    >=5.3 example

    $new = Fx::GroupBy(
        $arr, 
        array(
            function($elem){ return (string)$elem['companyCode'][0]; },
            function($elem){ return (string)$elem['name'][0]; },
            function($elem){ return (string)$elem['vehicleRentalPrefType'][0]; }
        ),
        array(
            null,
            null,
            function($elem){ return (float)$elem['rateAmount'][0]; }
        )
    );
    
    print_r($new);
    

    for PHP 5.2 and possibly under

    class Fx
    {
        public static function GroupBy(&$arr, $groupby, $valueFunctions = null)
        {
            $groupby = is_array($groupby) ? $groupby : array($groupby);
            $valueFunctions = is_array($valueFunctions) ? $valueFunctions : array($valueFunctions);
    
            $group1 = array();
            foreach($arr as $elem)
            {
                $gbIsFunc = (substr($groupby[0], 0, 5) == 'func:');
                $gbFunc = substr($groupby[0], 5);
    
                $vIsFunc = (substr($valueFunctions[0], 0, 5) == 'func:');
                $vFunc = substr($valueFunctions[0], 5);
    
                $group = $gbIsFunc ? $gbFunc($elem) : $elem[$groupby[0]];
                $group1[$group][] = $vIsFunc ? $vFunc($elem) : (is_null($valueFunctions[0]) ? $elem : $elem[$valueFunctions[0]]);
            }
            if(count($groupby) > 1)
            {
                $group2 = array();
                $gb_next = array_slice($groupby, 1);
                $vf_next = (count($valueFunctions) > 1) ? array_slice($valueFunctions, 1) : null;
                foreach($group1 as $group => $elems)
                {
                    $group2[$group] = self::GroupBy($elems, $gb_next, $vf_next);
                }
    
                return $group2;
            }
    
            return $group1;
        }
    }
    

    <=5.2 example

    function gbFunc1($elem){ return (string)$elem['companyCode'][0]; }
    function gbFunc2($elem){ return (string)$elem['name'][0]; }
    function gbFunc3($elem){ return (string)$elem['vehicleRentalPrefType'][0]; }
    function vFunc3($elem){ return (float)$elem['rateAmount'][0]; }
    
    $new = Fx::GroupBy($arr, array('func:gbFunc1', 'func:gbFunc2', 'func:gbFunc3'), array(null, null, 'func:vFunc3'));
    
    print_r($new);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to combine 2 parts of the same array to make a complex
I want to combine the [word] part of each array with , and the
I have three arrays of strings and i want to combine them and write
I want to combine three php arrays by taking one entry and placing it
I want to compare many arrays and combine any that are identical: A =
I want to combine two arrays like this: 1st array: array( ATTENDED => 1,
I want to combine three tables - date, lead and click - in a
I want to combine three sprites and display it as a single sprite. I
I want to combine Hadoop based Mahout recommenders with Apache Hive.So that My generated
I want to combine two array's, excluding duplicates. I am using a custom class:

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.