kohkimakimoto / earray Goto Github PK
View Code? Open in Web Editor NEWEArray is a small PHP class to provide convenient ways to access a PHP Array.
License: Other
EArray is a small PHP class to provide convenient ways to access a PHP Array.
License: Other
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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.