This repository was archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayUtil.php
More file actions
78 lines (72 loc) · 2.04 KB
/
ArrayUtil.php
File metadata and controls
78 lines (72 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/* THIS FILE:
* a) BELONGS TO THE 'PHP-UTIL' LIBRARY:
* https://github.com/thiagodp/php-util
*
* b) IS DISTRIBUTED UNDER THE CREATIVE COMMONS LICENCE (CC BY 3.0):
* http://creativecommons.org/licenses/by/3.0/
*
* USE IT AT YOUR OWN RISK!
*/
/**
* Array utilities.
*
* @author Thiago Delgado Pinto
* @version 1.2
*/
class ArrayUtil {
/**
* Check for non existing keys in an array.
*
* @param keys an array with the keys to check in the target array.
* @param targetArray the target array.
* @return an array with the keys not found.
*/
static function nonExistingKeys( array $keys, array $targetArray ) {
$notFound = array();
foreach ( $keys as $k ) {
if ( ! isset( $targetArray[ "$k" ] ) ) { // if ! array_key_exists( $k, $targetArray )
array_push( $notFound, $k );
}
}
return $notFound;
}
/**
* Removes an item from an array.
*
* @param array array the array with the item.
* @param unknown_type item the item to remove.
* @param boolean reindexArray option to reindex the array after removing. Default is true.
* @param boolean compareItemTypes option to compare the items with === instead of ==. Default is false.
* @return true if removed, false otherwise.
*/
static function removeItem( array &$array, $item, $reindexArray = true, $compareItemTypes = false ) {
if ( ! isset( $array ) || ! isset( $item ) ) {
return false;
}
$key = array_search( $item, $array, $compareItemTypes );
if ( false === $key ) {
return false;
}
unset( $array[ $key ] );
if ( $reindexArray ) {
$array = array_values( $array );
}
return true;
}
/**
* Replace the given keys in the target array.
*
* @param array $keyMap The key map (usually string to string)
* @param array $target The target array, passed by reference.
*/
static function replaceKeys( array $keyMap, array &$target ) {
foreach ( $keyMap as $oldKey => $newKey ) {
if ( isset( $target[ $oldKey ] ) ) {
$target[ $newKey ] = $target[ $oldKey ];
unset( $target[ $oldKey ] );
}
}
}
}
?>