Giter VIP home page Giter VIP logo

codeigniter-array-tree's Introduction

CodeIgniter Array Tree Helper

A CodeIgniter helper that outputs the given array as a nested array. This is useful when creating page trees, or passing in a flat array that has relationships in itself.

Installation

Save this script to application/helpers/ or wherever you are loading helpers. If this is for PyroCMS, you would put this in pyrocms/addons/shared_addons/helpers/

Using the Helper

This can be in your controller, model, or view (not preferable).

$this->load->helper('array_tree');

If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.

$autoload['helper'] = array('array_tree');

Usage

Input Code

$arr = array(
  array(
    'id' => 100,
    'parentid' => 0,
    'name' => 'a'
  ),
  array(
    'id' => 101,
    'parentid' => 100,
    'name' => 'a'
  ),
  array(
    'id' => 102,
    'parentid' => 101,
    'name' => 'a'
  ),
  array(
    'id' => 103,
    'parentid' => 101,
    'name' => 'a'
  )
);

// items which match their `id` to `parentid` are nested in a `children` array
$results = array_tree($arr, 'id', 'parentid', 'children');
print_r($results);

Output Results

Array
(
    [0] => Array
        (
            [id] => 100
            [parentid] => 0
            [name] => a
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 101
                            [parentid] => 100
                            [name] => a
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 102
                                            [parentid] => 101
                                            [name] => a
                                        )
                                    [1] => Array
                                        (
                                            [id] => 103
                                            [parentid] => 101
                                            [name] => a
                                        )
                                )
                        )
                )
        )
)

codeigniter-array-tree's People

Contributors

james2doyle avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.