(root)/shared-arrays.php - Rev 254
Rev 231 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of Travelsized CMS
* A content management system with modules, based on wiki syntax
*
* Author: Dan Jensen <admin@leinir.dk>
* Copyright 2003/2004
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License is available at: http://www.gnu.org/copyleft/
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function array_export
($array_to_export, $name_of_array) {
$encoded = "<?PHP
\$$name_of_array = " . var_export($array_to_export, true) . ";
?>";
return $encoded;
}
/**
* Merges two arrays of any dimension
*
* This is the process' core!
* Here each array is merged with the current resulting one
*
* @author Chema Barcala Calveiro <shemari75@mixmail.com>
* @param array Resulting array - passed by reference
* @param array_i Array to be merged - passed by reference
*/
function array_merge_2
(&$array, &$array_i) {
// For each element of the array (key => value):
foreach ($array_i as $k => $v) {
// If the value itself is an array, the process repeats recursively:
if (is_array($v)) {
if (!isset($array[$k])) {
$array[$k] = array();
}
array_merge_2
($array[$k], $v);
// Else, the value is assigned to the current element of the resulting array:
} else {
if (isset($array[$k]) && is_array($array[$k])) {
$array[$k][0] = $v;
} else {
if (isset($array) && !is_array($array)) {
$temp = $array;
$array = array();
$array[0] = $temp;
}
$array[$k] = $v;
}
}
}
}
/**
* Merges any number of arrays of any dimension
*
* The arrays to be merged are passed as arguments to the function,
* which uses an external function (array_merge_2) to merge each of them
* with the resulting one as it's being constructed
*
* @author Chema Barcala Calveiro <shemari75@mixmail.com>
* @return array Resulting array, once all have been merged
*/
function array_merge_n
() {
// Initialization of the resulting array:
$array = array();
// Arrays to be merged (function's arguments):
$arrays =& func_get_args();
// Merging of each array with the resulting one:
foreach ($arrays as $array_i) {
if (is_array($array_i)) {
array_merge_2
($array, $array_i);
}
}
return $array;
}
// Returns the key at the end of the array - thanks to jasper at jtey dot com on the php.net manual
function endKey
($array){
end($array);
return key($array);
}
function firstKey
($array){
reset($array);
return key($array);
}
/**
* Compares two array elements (for use with usort), but with further added functionality.
* You pass a column ID to the function, and wether the comparison is reversed (from larger to smaller)
*
* The following code sample shows two sample functions, which would sort an array according to the "title"
* column, in direct and reverse order respectively:
*
\code
function compare_title($x, $y) { return arrayColumnCompare($x, $y, "title"); }
function compare_titlerev($x, $y) { return arrayColumnCompare($x, $y, "title", true); }
\endcode
*
* @param x The first of the elements to compare
* @param y The second of the elements to compare
* @param column The key of the column to sort
* @param reverse Wether the search should be reversed. Default is to sort ascending
*
* @return Where to shift the array elements
*/
function arrayColumnCompare
($x, $y, $column, $reverse = false)
{
if ( $x[$column] == $y[$column] )
return 0;
else if ( $x[$column] < $y[$column] )
return $reverse ?
1 : -1;
else
return $reverse ?
-1 : 1;
}
?>