Coder Social home page Coder Social logo

earray's People

Contributors

kohkimakimoto avatar mloureiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

happyproff

earray's Issues

If you want to improve EArray, here is a deep sorting function

Hi,

Yes I should do a pull request but no I don't use Github as my repository so this is here instead.

Could you please add this to EArray so I don't have to keep this add-on to it.

Thanks

    public function sort($key)
    {
        $this->key = $key;
        uasort($this->array, function($one, $another)
        {
            $one = new EArray($one);
            $another = new EArray($another);
            return strcmp($one->get($this->key), $another->get($this->key));
        });
        unset($this->key);
    }

If you want to add $this->key by hand make it public but this can be dynamically created by this function here instead

key is the same format as the rest of the class obviously but without the first level as it is generic, and it allows to sort an array based on some deep subarray.
For instance

require("EArray.php");
$array["f"]["details"]["weight"] = 1;
$array["f"]["details"]["position"] = 6;
$array["e"]["details"]["weight"] = 2;
$array["e"]["details"]["position"] = 5;
$array["d"]["details"]["weight"] = 3;
$array["d"]["details"]["position"] = 4;
$array["c"]["details"]["weight"] = 4;
$array["c"]["details"]["position"] = 3;
$array["b"]["details"]["weight"] = 5;
$array["b"]["details"]["position"] = 2;
$array["a"]["details"]["weight"] = 6;
$array["a"]["details"]["position"] = 1;

$array = new Kohkimakimoto\EArray\EArray($array);
print_r($array);
$array->sort("details/position");
print_r($array);
Kohkimakimoto\EArray\EArray Object
(
    [array:protected] => Array
        (
            [f] => Array
                (
                    [details] => Array
                        (
                            [weight] => 1
                            [position] => 6
                        )

                )

            [e] => Array
                (
                    [details] => Array
                        (
                            [weight] => 2
                            [position] => 5
                        )

                )

            [d] => Array
                (
                    [details] => Array
                        (
                            [weight] => 3
                            [position] => 4
                        )

                )

            [c] => Array
                (
                    [details] => Array
                        (
                            [weight] => 4
                            [position] => 3
                        )

                )

            [b] => Array
                (
                    [details] => Array
                        (
                            [weight] => 5
                            [position] => 2
                        )

                )

            [a] => Array
                (
                    [details] => Array
                        (
                            [weight] => 6
                            [position] => 1
                        )

                )

        )

)
Kohkimakimoto\EArray\EArray Object
(
    [array:protected] => Array
        (
            [a] => Array
                (
                    [details] => Array
                        (
                            [weight] => 6
                            [position] => 1
                        )

                )

            [b] => Array
                (
                    [details] => Array
                        (
                            [weight] => 5
                            [position] => 2
                        )

                )

            [c] => Array
                (
                    [details] => Array
                        (
                            [weight] => 4
                            [position] => 3
                        )

                )

            [d] => Array
                (
                    [details] => Array
                        (
                            [weight] => 3
                            [position] => 4
                        )

                )

            [e] => Array
                (
                    [details] => Array
                        (
                            [weight] => 2
                            [position] => 5
                        )

                )

            [f] => Array
                (
                    [details] => Array
                        (
                            [weight] => 1
                            [position] => 6
                        )

                )

        )

)

PS : Note that this is for string comparison and that I did not implement a integer comparison.

Bug on recursive get value

Here I'm posting a fix when using custom delimiters. I'm using dot as delimiter. By the way, this class is awesome! Thanks

public function get($key, $default = null, $delimiter = null)
{
    if ($delimiter === null) {     <-----------
        $delimiter = $this->delimiter;      <-----------
    }       <-----------

    $ret = $this->getRawValue($key, $default, $delimiter);
    if (is_array($ret)) {
        $ret = new EArray($ret, $delimiter);     <----------
    }

    return $ret;
}

Sorry, formatting of text is not working for me.

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.