Coder Social home page Coder Social logo

earray's Introduction

EArray

Build Status Coverage Status Latest Stable Version

EArray is a small PHP class to provide convenient ways to access a PHP Array.

  • Convenient accessing a nested array.
  • Supporting a default value.
  • Supporting a normal array operation.
  • Implement sort functions.

It aims to remove code that checks array key existence. Especially for a nested array. Do you hate like the following code?

$val = null;
$arr2 = isset($arr["key"]) ? $arr["key"] : null;
if (is_array($arr2)) {
    $val = isset($arr2["key2"]) ? $arr2["key2"] : null;
}

echo $val;

You can write same things using EArray object.

$val = $earray->get("key/key2", null);
echo $val;

Requirement

PHP5.3 or later.

Installation

You can use composer installation. Make composer.json file like the following.

{
      "require": {
          "kohkimakimoto/earray": "2.0.*"
      }
}

And run composer install command.

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

Usage

Accessing array values

<?php
use Kohkimakimoto\EArray\EArray;

$earray = new EArray(array("foo" => "bar"));
$earray->get("foo");             # "bar"
$earray->get("foo2");            # null
$earray->get("foo2", "default"); # "default"

$earray->set("foo", "bar2");
$earray->get("foo");             # "bar2"

$earray->delete("foo");
$earray->get("foo");             # null

Accessing nested array values

<?php
use Kohkimakimoto\EArray\EArray;

$earray = new EArray(
    array(
        "foo" => array(
            "foo2" => array(
                "foo3",
                "foo4",
                ),
            "foo2-1" => "foo5",
            ),
        "bar",
        "hoge",
        )
);

// You can get value from a nested array using a delimiter (default "/")
$earray->get("foo/foo2-1");             # "foo5".
$earray->get("foo");                    # EArray(array("foo2" => array("foo3","foo4",),"foo2-1" => "foo5"))
$earray->get("foo")->get("foo2-1");     # "foo5".
$earray->get("foo")->toArray();         # array("foo2" => array("foo3","foo4",),"foo2-1" => "foo5")

// You can change a delimiter by the third argument.
$earray->get("foo.foo2-1", null, ".");  # "foo5"

// You can set a nested array using a delimiter
$earray->set("foo/foo2-1", "foo5-modify");
$earray->get("foo/foo2-1");             # "foo5-modify".

You can specify a default delemiter by constructor

<?php
use Kohkimakimoto\EArray\EArray;

$earray = new EArray(array("foo" => array("bar" => "value")), ".");

$earray->get("foo.bar"));    // "value"

Sort an array

<?php
use Kohkimakimoto\EArray\EArray;

$array = array();
$array["f"]["details"]["weight"] = 1;
$array["f"]["details"]["position"] = 34;
$array["e"]["details"]["weight"] = 2;
$array["e"]["details"]["position"] = 33;
$array["d"]["details"]["weight"] = 3;
$array["d"]["details"]["position"] = 22;
$array["c"]["details"]["weight"] = 4;
$array["c"]["details"]["position"] = 11;
$array["b"]["details"]["weight"] = 5;
$array["b"]["details"]["position"] = 2;
$array["a"]["details"]["weight"] = 6;
$array["a"]["details"]["position"] = 1;

$earray = new EArray($array);
print_r($earray->sort("details/position")->toArray());  // sort by details/position 

// Result
// array("a" => array(...), "b" => array(...), "c" => array(...), "d" => array(...), ...)

print_r($earray->rsort("details/position")->toArray());  // reverse sort by details/position 

// Result
// array("f" => array(...), "e" => array(...), "d" => array(...), "c" => array(...), ...)

Using like a normal array

<?php
use Kohkimakimoto\EArray\EArray;

$earray = new EArray(array(
    "foo" => "bar",
    "foo1" => "bar1",
    "foo2" => "bar2",
    "foo3" => "bar3",
    "foo4" => "bar4",
));

foreach ($earray as $k => $v) {
   echo $v;  # "bar", "bar1", "bar2", ...
}

License

Apache License 2.0

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.