$v) { $array[$k] = $v; } } return $array; } public static function mergeDiff($array1, $array2) { return array_merge($array1, array_diff_key($array2, $array1)); } public static function diffRecursive($array1, $array2) { foreach ($array1 as $key => $value) { if (is_array($value)) { if (@!is_array($array2[$key])) { $difference[$key] = $value; } else { $new_diff = Qs_Array::diffRecursive($value, $array2[$key]); if (!empty($new_diff)) { $difference[$key] = $new_diff; } } } elseif ($value != @$array2[$key]) { $difference[$key] = $value; } } return !isset($difference) ? array() : $difference; } public static function mergeRecursive($firstArray, $secondArray) { if (is_array($firstArray) && is_array($secondArray)) { foreach ($secondArray as $key => $value) { if (isset($firstArray[$key])) { $firstArray[$key] = Qs_Array::mergeRecursive($firstArray[$key], $value); } else { $firstArray[$key] = $value; } } } else { $firstArray = $secondArray; } return $firstArray; } /** * Array sorting * * $data, multidim array * $keys, array(array(key => col1, sort => desc), array(key => col2, type => numeric)) * * @param array $data * @param array $keys * @return array */ public static function multisort($data, $keys) { // List As Columns $cols = array(); foreach ($data as $key => $row) { foreach ($keys as $k){ $cols[$k['key']][$key] = $row[$k['key']]; } } // List original keys $idkeys = array_keys($data); // Sort Expression $i = 0; $sort = ''; foreach ($keys as $k) { if ($i > 0) { $sort .= ', '; } $sort .= '$cols[\'' . $k['key'] . '\']'; if (isset($k['sort'])) { $sort .= ', SORT_' . strtoupper($k['sort']); } if (isset($k['type'])) { $sort .= ', SORT_' . strtoupper($k['type']); } $i++; } $sort .= ', $idkeys'; // Sort Funct $sort = 'array_multisort(' . $sort . ');'; eval($sort); // Rebuild Full Array foreach($idkeys as $idkey) { $result[$idkey] = $data[$idkey]; } return $result; } public static function get($array, $field, $default = null) { if (false !== $field) { $field = str_replace(']', '', $field); $parts = explode('[', $field); while (($name = array_shift($parts))) { if (!array_key_exists($name, $array)) { return $default; } $array = $array[$name]; } } return $array; } public static function set(&$array, $field, $value) { if (false !== $field) { if (is_array($field)) { $parts = $field; } else { $field = str_replace(']', '', $field); $parts = explode('[', $field); } if (null !== ($name = array_shift($parts))) { if (empty($parts)) { $array[$name] = $value; } elseif (!array_key_exists($name, $array) || !is_array($array[$name])) { $array[$name] = array(); } Qs_Array::set($array[$name], $parts, $value); } } else { $array = $value; } } public static function remove(&$array, $field) { if (false !== $field) { if (is_array($field)) { $parts = $field; } else { $field = str_replace(']', '', $field); $parts = explode('[', $field); } if (($name = array_shift($parts))) { if (empty($parts)) { unset($array[$name]); } elseif (array_key_exists($name, $array)) { Qs_Array::remove($array[$name], $parts); } } } else { unset($array); } } public static function excludeArray($array1, $array2) { foreach ($array2 as $keyName) { if (is_array($keyName)) { $array1 = Qs_Array::excludeArray($array1, $keyName); } elseif (array_key_exists($keyName, $array1)) { unset($array1[$keyName]); } } return $array1; } public static function exclude() { if (func_num_args() < 2) { return false; } $args = func_get_args(); $array = array_shift($args); $array = Qs_Array::excludeArray($array, $args); return $array; } public static function collapse(array $array, $belongsTo = '') { $result = array(); foreach ($array as $key => $value) { $itemBelongsTo = $belongsTo . (empty($belongsTo) ? $key : '[' . $key . ']'); if (is_array($value)) { $result = array_merge($result, Qs_Array::collapse($value, $itemBelongsTo)); } else { $result[$itemBelongsTo] = $value; } } return $result; } public static function expand(array $array) { $result = array(); foreach ($array as $key => $value) { Qs_Array::set($result, $key, $value); } return $result; } /** * Swap 2 elements in array preserving keys * * @param array $array * @param array $key1 * @param array $key2 * @return array */ public static function swap($array, $key1, $key2) { if (!is_array($array) || empty($array) || !array_key_exists($key1, $array) || !array_key_exists($key2, $array) ) { return $array; } $value1 = $array[$key1]; $value2 = $array[$key2]; $result = array(); foreach($array as $key => $value) { if ((string) $key == (string) $key1) { $key = $key2; $value = $value2; } else if ((string) $key == (string) $key2) { $key = $key1; $value = $value1; } $result[$key] = $value; } return $result; } /** * Move an element inside an array preserving keys * $relpos should be like -1, +2... * * @param array $array * @param array $key * @param array $relpos * @return array */ public static function move($array, $key, $relpos) { if (!is_array($array) || empty($array) || !array_key_exists($key, $array) || !$relpos) { return $array; } $from = Qs_Array::keyPos($key, $array); if ($from === false) { return $array; } $to = $from + $relpos + ($relpos > 0 ? 1 : 0); $length = count($array); if ($to >= $length) { $val = $array[$key]; unset($array[$key]); $array[$key] = $val; } else { if ($to < 0) { $to = 0; } $new = array(); $x = 0; foreach ($array as $i => $v) { if ($x++ == $to) { $new[$key] = $array[$key]; } if ($i !== $key) { $new[$i]=$v; } } $array = $new; } return $array; } /** * Get a key position in array * * @param array $key * @param array $array * @return integer */ public static function keyPos($key, $array) { $position = 0; foreach ($array as $i => $v) { if ($key === $i) { return $position; } $position++; } return false; } /** * Return key by position * * @param array $key * @param array $array * @return integer */ public static function keyByPos($position, $array) { $x = 0; foreach ($array as $i => $v) { if ($position == $x++) { return $i; } } return false; } }