Project continued with rewrites:
[mailer.git] / inc / wrapper-functions.php
index aaa89c15ddb53d2d03909ed1188ba5fb7c82e5fa..dbc24d16796050d6e84cd70f73d72a078014ead9 100644 (file)
@@ -157,7 +157,7 @@ function decodeEntities ($str, $quote = ENT_NOQUOTES) {
 }
 
 // Merges an array together but only if both are arrays
-function merge_array ($array1, $array2) {
+function merge_array ($array1, $array2, $keepIndex = FALSE) {
        // Are both an array?
        if ((!is_array($array1)) && (!is_array($array2))) {
                // Both are not arrays
@@ -170,8 +170,20 @@ function merge_array ($array1, $array2) {
                reportBug(__FUNCTION__, __LINE__, sprintf("array2 is not an array. array != %s", gettype($array2)));
        }
 
-       // Merge both together
-       return array_merge($array1, $array2);
+       // Maintain index of array2?
+       if ($keepIndex === TRUE) {
+               // Keep index of array2, array_merge() rewrites $key=2 to $key=0 ! :(
+               foreach ($array2 as $key => $value) {
+                       // Add it
+                       $array1[$key] = $value;
+               } // END - foreach
+
+               // Return it
+               return $array1;
+       } else {
+               // Merge both together normally
+               return array_merge($array1, $array2);
+       }
 }
 
 // Check if given FQFN is a readable file
@@ -2867,33 +2879,41 @@ function parseFloat ($floatString){
  * Searches a multi-dimensional array (as used in many places) for given
  * key/value pair as taken from user comments from PHP documentation website.
  *
- * @param      $array          An array with one or more dimensions
- * @param      $key            Key to look for
- * @param      $valur          Value to look for
- * @return     $results        Resulted array or empty array if $array is no array
+ * @param      $array                  An array with one or more dimensions
+ * @param      $key                    Key to look for
+ * @param      $value                  Value to look for
+ * @param      $parentIndex    Parent index (ONLY INTERNAL USE!)
+ * @return     $results                Resulted array or empty array if $array is no array
  * @author     sunelbe<at>gmail<dot>com
  * @link       http://de.php.net/manual/en/function.array-search.php#110120
  */
-function search_array ($array, $key, $value) {
+function search_array ($array, $key, $value, $parentIndex = NULL) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'array(' . count($array) . ')=' . print_r($array, TRUE) . ',key=' . $key . ',value=' . $value . ',parentIndex[' . gettype($parentIndex) . '=' . $parentIndex . ' - ENTERED!');
        // Init array result
        $results = array();
 
        // Is $array really an array?
        if (is_array($array)) {
-               // Does key and value match?
-               if (isset($array[$key]) && $array[$key] == $value) {
-                       // Then add it as result
-                       $results[] = $array;
-               } // END - if
-
                // Search for whole array
-               foreach ($array as $subArray) {
-                       // Search recursive and merge again
-                       $results = merge_array($results, search_array($subArray, $key, $value));
+               foreach ($array as $idx => $dummy) {
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',idx=' . $idx);
+                       // Is dummy an array?
+                       if (is_array($dummy)) {
+                               // Then search again
+                               $subResult = search_array($dummy, $key, $value, $idx);
+                               //* DEBUG: */ print 'subResult=<pre>' . print_r($subResult, TRUE).'</pre>';
+
+                               // And merge both
+                               $results = merge_array($results, $subResult, TRUE);
+                       } elseif ((isset($array[$key])) && ($array[$key] == $value)) {
+                               // Is found, so add it
+                               $results[$parentIndex] = $array;
+                       }
                } // END - foreach
        } // END - if
 
        // Return resulting array
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'results(' . count($results) . ')=' . print_r($results, TRUE) . ' - EXIT!');
        return $results;
 }