Skip to content

Commit 9bd16b7

Browse files
authored
Update ArrayHelper.php
1 parent 14067b9 commit 9bd16b7

File tree

1 file changed

+72
-39
lines changed

1 file changed

+72
-39
lines changed

src/ArrayHelper.php

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,69 @@ public static function __callStatic($name, $params){
3333
return call_user_func_array([new self($input), $name], $params);
3434
}
3535

36+
public function combine(array $keys = null, array $values = null){
37+
return array_combine(null === $keys ? array_keys($this->arr) : $keys, null === $values ? array_values($this->arr) : $values);
38+
}
39+
public function setKeys(array $keys){
40+
return new self($this->combine($keys, array_values($this->arr)));
41+
}
42+
public function setValues(array $values){
43+
return new self($this->combine(array_keys($this->arr), $values));
44+
}
45+
public function toDotTree(string $prefix = '{'/*'${'*/, string $suffix = '}'){
46+
return \frdl\Context::create($this->arr)
47+
->pfx($prefix)
48+
->sfx($suffix)
49+
;
50+
}
51+
public function findIn($column, $search_value,array $arr = null) {
52+
$arr = (!is_null($arr)) ? $arr : $this->arr;
53+
$colors = array_column($arr, $column);
54+
$found_key = array_search($search_value, $arr);
55+
return new self($arr[$found_key]);
56+
}
57+
58+
public function getByHash($keymap,$hashIndex = null){
59+
60+
$nest_depth = sizeof($keymap);
61+
if(null===$hashIndex){
62+
$hashIndex=max(0,$nest_depth-1);
63+
}
64+
if(is_int($hashIndex)){
65+
$hashIndex=max($hashIndex,$nest_depth);
66+
}
67+
$value = $this->arr;
68+
for ($i = 0; $i < $nest_depth; $i++) {
69+
$value = $value[$keymap[$i]];
70+
if(is_int($hashIndex) && $hashIndex === $i || $hashIndex === $keymap[$i])break;
71+
72+
}
73+
74+
return $value;
75+
}
76+
77+
78+
public function find($search_value, $data = null, $hashIndex = null) {
79+
return $this->getByHash($this->hash($search_value), $hashIndex);
80+
}
81+
public function hash($needle) {
82+
return self::getHash($needle, $this->arr) ;
83+
}
84+
public static function getHash($needle, $haystack) {
85+
foreach($haystack as $first_level_key=>$value) {
86+
if ($needle === $value || preg_match('/^'.$needle.'$\/', $value)) {
87+
return array($first_level_key);
88+
} elseif (is_array($value)) {
89+
$callback = self::getHash($needle, $value);
90+
if ($callback) {
91+
return array_merge(array($first_level_key), $callback);
92+
}
93+
}
94+
}
95+
return false;
96+
}
97+
98+
3699
/*
37100
Example:
38101
<?php
@@ -225,51 +288,21 @@ public static function paginate($input, $page, int $show_per_page = 10) {
225288
return (new self($input))->chunk($page, $show_per_page);
226289
}
227290

228-
public function getByHash($keymap,$hashIndex = null){
229-
230-
$nest_depth = sizeof($keymap);
231-
if(null===$hashIndex){
232-
$hashIndex=max(0,$nest_depth-1);
233-
}
234-
if(is_int($hashIndex)){
235-
$hashIndex=max($hashIndex,$nest_depth);
236-
}
237-
$value = $this->arr;
238-
for ($i = 0; $i < $nest_depth; $i++) {
239-
$value = $value[$keymap[$i]];
240-
if(is_int($hashIndex) && $hashIndex === $i || $hashIndex === $keymap[$i])break;
241-
242-
}
243291

244-
return $value;
245-
}
246-
247-
public function find($search_value, $data = null, $hashIndex = null) {
248-
return $this->getByHash($this->hash($search_value), $hashIndex);
249-
}
250-
public function hash($needle) {
251-
return self::getHash($needle, $this->arr) ;
252-
}
253-
public static function getHash($needle, $haystack) {
254-
foreach($haystack as $first_level_key=>$value) {
255-
if ($needle === $value || preg_match('/^'.$needle.'$\/', $value)) {
256-
return array($first_level_key);
257-
} elseif (is_array($value)) {
258-
$callback = self::getHash($needle, $value);
259-
if ($callback) {
260-
return array_merge(array($first_level_key), $callback);
261-
}
262-
}
263-
}
264-
return false;
265-
}
266-
267-
292+
public function all(){
293+
return $this->index;
294+
}
295+
/*
268296
269297
public static function before(array $src,array $in, $pos){
270298
$this->index= ((!is_int($pos)) ? ArrayHelper::getHash($pos, $this->arr)[0] : $pos) -1;
271299
return $this;
272300
}
301+
*/
302+
public function before($pos){
303+
$this->index= ((!is_int($pos)) ? ArrayHelper::getHash($pos, $this->arr)[0] : $pos) -1;
304+
return $this;
305+
}
273306

274307
public function after( $pos){
275308
$this->index= ((!is_int($pos)) ? ArrayHelper::getHash($pos, $this->arr)[0] : $pos) + 1;

0 commit comments

Comments
 (0)