Skip to main content

PHP array_merge vs +, a case study on recursive array as tree

    private function extractSubCategories($categories)

    {

        $result = [];

        foreach ($categories as $key => $node) {

            if (empty($node['sub_categories'])) {

                $result[$node['id']] = $node;

            } else {

                $tmp = $node['sub_categories'];

                unset($node['sub_categories']);

                $result[$node['id']] = $node;

                $child = $this->extractSubCategories($tmp);

                $result = $result + $child;

            }

        }


        return $result;

    }


More detail ...

Damn it! Simple change array_merge to '+'


// Second way

        // foreach ($debug as $key => $node) {

        //     if (empty($node['sub_categories'])) {

        //         $result[$node['id']] = $node;

        //     } else {

        //         $secondStep = $secondStep + $node['sub_categories'];

        //     }

        // }

        // // dd($result);

        // dd($secondStep);

manually many steps.


        $debug = array_slice($data, 0, 7, true);




        // dd($debug);

        // echo '<pre>'. var_export($debug) . '</pre>';

        // $result = $this->extractSubCategories($debug);


slice($data, 0, 3, true) vs $data['1'] since its key retain.


JSON tree bonsai sample:


   "22":{

      "id":"22",

      "title":"OEM Rotor",

      "caption":"OEM Rotor",

      "parentId":"1",

      "sub_categories":[

         

      ],

      "childCount":0

   },

   "23":{

      "id":"23",

      "title":"Drilled Rotor",

      "caption":"Drilled Rotor",

      "parentId":"1",

      "sub_categories":[

      ],

      "childCount":0

   }
}


Code convert nested tree array to plain array (used for Bonsai JSON)

Recursive function;

    private function extractSubCategories($categories)

    {

        $result = [];

        foreach ($categories as $key => $node) {

            if (empty($node['sub_categories'])) {

                $result[$node['id']] = $node;

            } else {

                $tmp = $node['sub_categories'];

                unset($node['sub_categories']);

                $result[$node['id']] = $node;

                $child = $this->extractSubCategories($tmp);

                $result = $result + $child; // array_merge not work, it lost array keys 

            }

        }


        return $result;

    }

Nested PHP array look like:

Array
(
    [1] => Array
        (
            [id] => 1
            [title] => eLINE Brake Rotors
            [caption] => eLINE Series Brake Rotors
            [parentId] => 
            [sub_categories] => Array
                (
                    [22] => Array
                        (
                            [id] => 22
                            [title] => OEM Rotor
                            [caption] => OEM Rotor
                            [parentId] => 1
                            [sub_categories] => Array
                                (
                                )

                            [childCount] => 0
                        )

                    [23] => Array
                        (
                            [id] => 23
                            [title] => Drilled Rotor
                            [caption] => Drilled Rotor
                            [parentId] => 1
                            [sub_categories] => Array
                                (
                                )

                            [childCount] => 0
                        )
        )
...
)


Comments

Popular posts from this blog

AWS Elasticache Memcached connection

https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/accessing-elasticache.html#access-from-outside-aws http://hourlyapps.blogspot.com/2010/06/examples-of-memcached-commands.html Access memcached https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/GettingStarted.AuthorizeAccess.html Zip include hidden file https://stackoverflow.com/questions/12493206/zip-including-hidden-files phpmemcachedadmin ~ phpMyAdmin or phpPgAdmin ... telnet mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com 11211 stats items stats cachedump 27 100 https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/VPCs.EC.html https://lzone.de/cheat-sheet/memcached VPC ID Security Group ID (sg-...) Cluster: The identifier for the cluster memcached1 Creation Time: The time (UTC) when the cluster was created January 9, 2019 at 11:47:16 AM UTC+7 Configuration Endpoint: The configuration endpoint of the cluster memcached1.ahgofe.cfg.usw1.cache.amazonaws.com:11211 St...

Notes Windows 10 Virtualbox config, PHP Storm Japanese, custom PHP, Apache build, Postgresql

 cmd => Ctrl + Shift + Enter mklink "C:\Users\HauNT\Videos\host3" "C:\Windows\System32\drivers\etc\hosts" https://www.quora.com/How-to-create-a-router-in-php https://serverfault.com/questions/225155/virtualbox-how-to-set-up-networking-so-both-host-and-guest-can-access-internet 1 NAT + 1 host only config https://unix.stackexchange.com/questions/115464/how-to-properly-set-up-2-network-interfaces-in-centos-running-in-virtualbox DEVICE=eth0 TYPE=Ethernet #BOOTPROTO=dhcp BOOTPROTO=none #IPADDR=10.9.11.246 #PREFIX=24 #GATEWAY=10.9.11.1 #IPV4_FAILURE_FATAL=yes #HWADDR=08:00:27:CC:AC:AC ONBOOT=yes NAME="System eth0" [root@localhost www]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 # Advanced Micro Devices, Inc. [AMD] 79c970 [PCnet32 LANCE] DEVICE=eth1 IPADDR=192.168.56.28 <= no eff => auto like DHCP #GATEWAY=192.168.56.1 #BOOTPROTO=dhcp BOOTPROTO=static <= no eff ONBOOT=yes HWADDR=08:00:27:b4:20:10 [root@localhost www]# ...

Rocket.Chat DB schema

_raix_push_notifications avatars.chunks avatars.files instances meteor_accounts_loginServiceConfiguration meteor_oauth_pendingCredentials meteor_oauth_pendingRequestTokens migrations rocketchat__trash rocketchat_cron_history rocketchat_custom_emoji rocketchat_custom_sounds rocketchat_import rocketchat_integration_history rocketchat_integrations rocketchat_livechat_custom_field rocketchat_livechat_department rocketchat_livechat_department_agents rocketchat_livechat_external_message rocketchat_livechat_inquiry rocketchat_livechat_office_hour rocketchat_livechat_page_visited rocketchat_livechat_trigger rocketchat_message rocketchat_oauth_apps rocketchat_oembed_cache rocketchat_permissions rocketchat_raw_imports rocketchat_reports rocketchat_roles rocketchat_room rocketchat_settings rocketchat_smarsh_history rocketchat_statistics rocketchat_subscription rocketchat_uploads system.indexes users usersSessions https://rocket.chat/docs/developer-guides/sc...