root/trunk/lib/SGL/Array.php
Revision 1965, 5.3 kB (checked in by demian, 1 year ago) | |
---|---|
|
Line | |
---|---|
1 | <?php |
2 | /* Reminder: always indent with 4 spaces (no tabs). */ |
3 | // +---------------------------------------------------------------------------+ |
4 | // | Copyright (c) 2006, Demian Turner | |
5 | // | All rights reserved. | |
6 | // | | |
7 | // | Redistribution and use in source and binary forms, with or without | |
8 | // | modification, are permitted provided that the following conditions | |
9 | // | are met: | |
10 | // | | |
11 | // | o Redistributions of source code must retain the above copyright | |
12 | // | notice, this list of conditions and the following disclaimer. | |
13 | // | o Redistributions in binary form must reproduce the above copyright | |
14 | // | notice, this list of conditions and the following disclaimer in the | |
15 | // | documentation and/or other materials provided with the distribution. | |
16 | // | o The names of the authors may not be used to endorse or promote | |
17 | // | products derived from this software without specific prior written | |
18 | // | permission. | |
19 | // | | |
20 | // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 | // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
23 | // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
24 | // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
25 | // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
26 | // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
27 | // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
28 | // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
30 | // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | // | | |
32 | // +---------------------------------------------------------------------------+ |
33 | // | Seagull 0.6 | |
34 | // +---------------------------------------------------------------------------+ |
35 | // | Array.php | |
36 | // +---------------------------------------------------------------------------+ |
37 | // | Author: Demian Turner <demian@phpkitchen.com> | |
38 | // +---------------------------------------------------------------------------+ |
39 | |
40 | /** |
41 | * Provides array manipulation methods. |
42 | * |
43 | * @package SGL |
44 | */ |
45 | class SGL_Array |
46 | { |
47 | /** |
48 | * Strips 'empty' elements from supplied array. |
49 | * |
50 | * 'Empty' can be a null, empty string, false or empty array. |
51 | * |
52 | * @param array $elem |
53 | * @return array |
54 | */ |
55 | function removeBlanks($elem) |
56 | { |
57 | if (is_array($elem)) { |
58 | $clean = array_filter($elem); |
59 | } else { |
60 | SGL::raiseError('array argument expected, got '.gettype($elem), |
61 | SGL_ERROR_INVALIDARGS); |
62 | } |
63 | return $clean; |
64 | } |
65 | |
66 | /** |
67 | * Returns an array with imploded keys. |
68 | * |
69 | * @param string $glue |
70 | * @param array $hash |
71 | * @param string $valwrap |
72 | * @return string |
73 | */ |
74 | function implodeWithKeys($glue, $hash, $valwrap='') |
75 | { |
76 | if (is_array($hash) && count($hash)) { |
77 | foreach ($hash as $key => $value) { |
78 | $aResult[] = $key.$glue.$valwrap.$value.$valwrap; |
79 | } |
80 | $ret = implode($glue, $aResult); |
81 | } else { |
82 | $ret = ''; |
83 | } |
84 | return $ret; |
85 | } |
86 | |
87 | /** |
88 | * Merges two arrays and replace existing entrys. |
89 | * |
90 | * Merges two Array like the PHP Function array_merge_recursive. |
91 | * The main difference is that existing keys will be replaced with new values, |
92 | * not combined in a new sub array. |
93 | * |
94 | * Usage: |
95 | * $newArray = SGL_Array::mergeReplace($array, $newValues); |
96 | * |
97 | * @access puplic |
98 | * @param array $array First Array with 'replaceable' Values |
99 | * @param array $newValues Array which will be merged into first one |
100 | * @return array Resulting Array from replacing Process |
101 | */ |
102 | function mergeReplace($array, $newValues) |
103 | { |
104 | foreach ($newValues as $key => $value) { |
105 | if (is_array($value)) { |
106 | if (!isset($array[$key])) { |
107 | $array[$key] = array(); |
108 | } |
109 | $array[$key] = SGL_Array::mergeReplace($array[$key], $value); |
110 | } else { |
111 | if (isset($array[$key]) && is_array($array[$key])) { |
112 | $array[$key][0] = $value; |
113 | } else { |
114 | if (isset($array) && !is_array($array)) { |
115 | $temp = $array; |
116 | $array = array(); |
117 | $array[0] = $temp; |
118 | } |
119 | $array[$key] = $value; |
120 | } |
121 | } |
122 | } |
123 | return $array; |
124 | } |
125 | } |
126 | ?> |
Note: See TracBrowser for help on using the browser.