Skip to content

Commit 8a4dee3

Browse files
committedSep 26, 2014
Init commit
0 parents  commit 8a4dee3

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
 

‎.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
phpunit.xml
2+
vendor/
3+
composer.lock
4+
clover.xml
5+
.idea/

‎LICENSE.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
===============
3+
4+
Copyright (c) 2014 Jesse G. Donat
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

‎composer.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "quorum/array-functions",
3+
"description": "useful array functions",
4+
"license": "MIT",
5+
"authors": [{
6+
"name": "Jesse Donat",
7+
"email": "donatj@gmail.com"
8+
}],
9+
"require-dev":{
10+
"phpunit/phpunit" : "~4.1"
11+
},
12+
"autoload": {
13+
"files": [ "src/array.php" ]
14+
}
15+
}

‎src/array.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/**
4+
* Given an array, find all the values recursively.
5+
*
6+
* @param array $array The Array to be Flattened
7+
* @param bool $allow_duplicates Should the array allow duplicates
8+
* @return array The resulting array or NULL on failure
9+
*/
10+
function array_flatten( array $array, $allow_duplicates = false ) {
11+
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
12+
$final = array();
13+
foreach( $it as $v ) {
14+
if( $allow_duplicates ) {
15+
$final[] = $v;
16+
} else {
17+
$final[$v] = $v;
18+
}
19+
}
20+
21+
return $final;
22+
}
23+
24+
/**
25+
* Given an array of arrays, merges the array's children together.
26+
*
27+
* @param array $arrays An array of arrays.
28+
* @param array|null $keys The merged array.
29+
* @return array
30+
*/
31+
function array_blend( array $arrays, array $keys = null ) {
32+
if( !is_array($arrays) ) return array();
33+
$out = array();
34+
35+
foreach( $arrays as $key => $array ) {
36+
if( is_array($array) && (is_null($keys) || (in_array($key, $keys))) ) {
37+
foreach( $array as $value ) {
38+
$out [] = $value;
39+
}
40+
}
41+
}
42+
43+
return $out;
44+
}
45+
46+
/**
47+
* Given an array of similarly keyed arrays, returns an array of only the values of the key.
48+
*
49+
* @param array $arrays An array of similarly keyed arrays
50+
* @param int|string $key the desired key
51+
* @return array the flattened array
52+
*/
53+
function array_key_array( array $arrays, $key ) {
54+
if( !is_array($arrays) ) {
55+
return array();
56+
}
57+
$out = array();
58+
foreach( $arrays as $i => $array ) {
59+
$out[$i] = $array[$key];
60+
}
61+
62+
return $out;
63+
}
64+
65+
/**
66+
* Given an array of similarly keyed arrays, returns an array of only the selected keys.
67+
*
68+
* @param array $arrays
69+
* @param array|int|string $keys The key or array of keys to return
70+
* @return array
71+
*/
72+
function array_keys_array( array $arrays, $keys ) {
73+
$keys = (array)$keys;
74+
75+
$out = array();
76+
foreach( $arrays as $i => $array ) {
77+
foreach( $keys as $index ) {
78+
$out[$i][$index] = $array[$index];
79+
}
80+
}
81+
82+
return $out;
83+
}
84+
85+
/**
86+
* Given a keyed array, fills any missing values.
87+
*
88+
* @param array $array A Keyed array
89+
* @param array $keys The keys that must exist
90+
* @param mixed $fill The desired value to fill with
91+
* @return array
92+
*/
93+
function array_key_refill( array $array, $keys, $fill = array() ) {
94+
foreach( $keys as $key ) {
95+
if( !isset($array[$key]) ) {
96+
$array[$key] = $fill;
97+
}
98+
}
99+
100+
return $array;
101+
}
102+

0 commit comments

Comments
 (0)