Skip to content

Commit c2f1329

Browse files
committed
if( !function_exists 's added
1 parent 4e044bd commit c2f1329

File tree

1 file changed

+88
-79
lines changed

1 file changed

+88
-79
lines changed

src/array.php

+88-79
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,111 @@
11
<?php
22

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;
3+
if( !function_exists('array_flatten') ) {
4+
/**
5+
* Given an array, find all the values recursively.
6+
*
7+
* @param array $array The Array to be Flattened
8+
* @param bool $allow_duplicates Should the array allow duplicates
9+
* @return array The resulting array or NULL on failure
10+
*/
11+
function array_flatten( array $array, $allow_duplicates = false ) {
12+
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
13+
$final = array();
14+
foreach( $it as $v ) {
15+
if( $allow_duplicates ) {
16+
$final[] = $v;
17+
} else {
18+
$final[$v] = $v;
19+
}
1820
}
19-
}
2021

21-
return $final;
22+
return $final;
23+
}
2224
}
2325

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();
26+
if( !function_exists('array_blend') ) {
27+
/**
28+
* Given an array of arrays, merges the array's children together.
29+
*
30+
* @param array $arrays An array of arrays.
31+
* @param array|null $keys The merged array.
32+
* @return array The resulting blended array
33+
*/
34+
function array_blend( array $arrays, array $keys = null ) {
35+
if( !is_array($arrays) ) return array();
36+
$out = array();
3437

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;
38+
foreach( $arrays as $key => $array ) {
39+
if( is_array($array) && (is_null($keys) || (in_array($key, $keys))) ) {
40+
foreach( $array as $value ) {
41+
$out [] = $value;
42+
}
3943
}
4044
}
41-
}
4245

43-
return $out;
46+
return $out;
47+
}
4448
}
4549

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-
}
50+
if( !function_exists('array_key_array') ) {
51+
/**
52+
* Given an array of similarly keyed arrays, returns an array of only the values of the key.
53+
*
54+
* @param array $arrays An array of similarly keyed arrays
55+
* @param int|string $key The desired key
56+
* @return array The flattened array
57+
*/
58+
function array_key_array( array $arrays, $key ) {
59+
if( !is_array($arrays) ) {
60+
return array();
61+
}
62+
$out = array();
63+
foreach( $arrays as $i => $array ) {
64+
$out[$i] = $array[$key];
65+
}
6166

62-
return $out;
67+
return $out;
68+
}
6369
}
6470

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;
71+
if( !function_exists('array_blend') ) {
72+
/**
73+
* Given an array of similarly keyed arrays, returns an array of only the selected keys.
74+
*
75+
* @param array $arrays An array of similarly keyed arrays
76+
* @param array|int|string $keys The key or array of keys to return
77+
* @return array The array of arrays with just the selected keys
78+
*/
79+
function array_keys_array( array $arrays, $keys ) {
80+
$keys = (array)$keys;
7481

75-
$out = array();
76-
foreach( $arrays as $i => $array ) {
77-
foreach( $keys as $index ) {
78-
$out[$i][$index] = $array[$index];
82+
$out = array();
83+
foreach( $arrays as $i => $array ) {
84+
foreach( $keys as $index ) {
85+
$out[$i][$index] = $array[$index];
86+
}
7987
}
80-
}
8188

82-
return $out;
89+
return $out;
90+
}
8391
}
8492

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;
93+
if( !function_exists('array_key_refill') ) {
94+
/**
95+
* Given a keyed array, fills any missing values.
96+
*
97+
* @param array $array A Keyed array
98+
* @param array $keys The keys that must exist
99+
* @param mixed $fill The desired value to fill with
100+
* @return array
101+
*/
102+
function array_key_refill( array $array, $keys, $fill = array() ) {
103+
foreach( $keys as $key ) {
104+
if( !isset($array[$key]) ) {
105+
$array[$key] = $fill;
106+
}
97107
}
98-
}
99108

100-
return $array;
109+
return $array;
110+
}
101111
}
102-

0 commit comments

Comments
 (0)