* $data - Multidimensional array to be keyed
* $keys - List containing the index/key(s) to use.
* $dupl - How to handle rows containing the same values. TRUE stores it as an Array, FALSE overwrites the previous row.
*
* Returns a multidimensional array indexed by $keys, or NULL if error.
* The number of dimensions is equal to the number of $keys provided (+1 if $dupl=TRUE).
*/ { // Sanity check if (!is_array($data)) return null;
// Allow passing single key as a scalar if (is_string($keys) or is_integer($keys)) $keys = Array($keys);
elseif (!is_array($keys)) return null;
// Our output array $out = Array();
// Loop through each row of our input $data foreach($data as $cx => $row) if (is_array($row))
{
// Loop through our $keys foreach($keys as $key)
{ $value = $row[$key];
if (!isset($last)) // First $key only {
if (!isset($out[$value])) $out[$value] = Array(); $last =& $out; // Bind $last to $out }
else // Second and subsequent $key.... {
if (!isset($last[$value])) $last[$value] = Array();
}
// Bind $last to one dimension 'deeper'.
// First lap: was &$out, now &$out[...]
// Second lap: was &$out[...], now &$out[...][...]
// Third lap: was &$out[...][...], now &$out[...][...][...]