Falls jemand mal ein Array bei Verwendung von Schlüsseln verschachteln muss:
array array_merge_recursive_bykeys ( array $source , array $para [, bool $check_on_duplicate_key = true, array &$unassigned ] )
Parameters
$source
The input array
$para
string [0] => target key
string [1] => extends key
string [2] => sort key
string [3] => create sub level key
$check_on_duplicate_key
throw Exception on duplicate key (default true)
if false the duplicates will be ignored
$unassigned
unassigned levels will be pushed into the given array
Return Values
An array of values resulted from merging/sorting.
Examples
PHP
// Example 1:
$source = array(
'irrelevant_1' => array( // key will be ignored
'name' => 'array 1', // target key
'position' => 2, // array will be set to level (key) 2
'extends' => 0, // array extends no other array (0 or '0' or '')
),
'irrelevant_2' => array( // key will be ignored
'name' => 'array 2', // target key
'position' => 1, // array will be set to level (key) 1
'extends' => 0, // array extends no other array (0 or '0' or '')
),
'irrelevant_4' => array( // key will be ignored
'name' => 'sub array 1', // target key
'position' => 1, // array will be set to level (key) 1
'extends' => 'array 2', // array extends another array containing the target key "array 2"
),
'irrelevant_5' => array( // key will be ignored
'name' => 'sub array 2', // target key
'position' => 2, // array will be set to level (key) 2
'extends' => 'array 2', // array extends another array containing the target key "array 2"
),
'irrelevant_6' => array( // key will be ignored
'name' => 'sub array', // target key
'position' => 13, // array will be set to level (key) 13
'extends' => 'sub array 2', // array extends another array containing the target key "sub array2"
),
// next array extends a non existing target key and will be pushed into array $unassigned (if given)
'irrelevant_3' => array(
'name' => 'failtest',
'position' => 1,
'extends' => 'failtest',
),
);
$key_extend_target = 'name';
$key_extends = 'extends';
$key_sortby = 'position';
$key_create_sub = 'i have sub menus';
$result = array_merge_recursive_bykeys(
$source,
array($key_extend_target, $key_extends, $key_sortby, $key_create_sub),
true,
$unassigned
);
// Output:
/*Array
(
[1] => Array
(
[name] => array 2
[position] => 1
[extends] => 0
[i have sub menus] => Array
(
[1] => Array
(
[name] => sub array 1
[position] => 1
[extends] => array 2
)
[2] => Array
(
[name] => sub array 2
[position] => 2
[extends] => array 2
[i have sub menus] => Array
(
[13] => Array
(
[name] => sub array
[position] => 13
[extends] => sub array 2
)
)
)
)
)
[2] => Array
(
[name] => array 1
[position] => 2
[extends] => 0
)
)*/
// Example 2:
$source = array(
array(
'name' => 'array 1', // target key
'position' => 1, // array will be set to level (key) 1
'extends' => 0, // array extends no other array
),
array(
'name' => 'array 2', // target key
'position' => 'foo', // array will be set to level (key) "foo"
'extends' => 'array 1', // array extends another array containing the target key "array 1"
),
array(
'name' => 'array 3', // target key
'position' => 'bar', // array will be set to level (key) "bar"
'extends' => 'array 2', // array extends another array containing the target key "array 1"
),
);
$key_extend_target = 'name';
$key_extends = 'extends';
$key_sortby = 'position';
$key_create_sub = 'i have sub menus';
$result = array_merge_recursive_bykeys(
$source,
array($key_extend_target, $key_extends, $key_sortby, $key_create_sub),
true,
$unassigned
);
// Output:
/*Array
(
[1] => Array
(
[name] => array 1
[position] => 1
[extends] => 0
[i have sub menus] => Array
(
[foo] => Array
(
[name] => array 2
[position] => foo
[extends] => array 1
[i have sub menus] => Array
(
[bar] => Array
(
[name] => array 3
[position] => bar
[extends] => array 2
)
)
)
)
)
)*/
Alles anzeigen
Die Function:
PHP
function array_merge_recursive_bykeys(array $source,array $para,$check_on_duplicate_key=true,&$unassigned=null){
if(empty($source)){
return $source;
}
if(
empty($para)or count($para) != 4
or !isset($para[0])or !isset($para[1])or !isset($para[2])or !isset($para[3])
){
throw new Exception('Error: expected parameter 2 (array) to have 4 level ([0]-[3]), '.count($para).' given!');
}
$n = count($source);
if($unassigned !== null and !is_array($unassigned)){
$unassigned = array($unassigned);
}
$result = array();
$tomerge = array();
$key_extend_target = $para[0];
$key_extends = $para[1];
$key_sortby = $para[2];
$key_create_sub = $para[3];
if($check_on_duplicate_key){
$func_error_on_duplicate_key = function($str){
throw new Exception('Error: '.$str);
};
}else{
$func_error_on_duplicate_key = function($str=null){
return;
};
}
$func_rec_sort;
$func_rec_sort = function(&$result,$toplace,$para,&$func_rec_sort,&$func_error_on_duplicate_key){
$key_extend_target = $para[0];
$key_extends = $para[1];
$key_sortby = $para[2];
$key_create_sub = $para[3];
foreach($result as $key => &$arr){
if($arr[$key_extend_target] === $toplace[$key_extends]){
if(isset($arr[$key_create_sub][$toplace[$key_sortby]])){
$func_error_on_duplicate_key(
'duplicate sorting key given at target `'.$arr[$key_extend_target].'`!'.
' (key `'.$key_sortby.'`, value `'.$toplace[$key_sortby].'`)'
);
}else{
$arr[$key_create_sub][$toplace[$key_sortby]] = $toplace;
ksort($arr[$key_create_sub]);
return true;
}
}
elseif(
isset($arr[$key_create_sub])
and $func_rec_sort($arr[$key_create_sub],$toplace,$para,$func_rec_sort,$func_error_on_duplicate_key)
){
return true;
}
}
return false;
};
foreach($source as $key => $arr){
if(
!isset($arr[$key_extends])
or $arr[$key_extends] === 0
or $arr[$key_extends] === ''
or $arr[$key_extends] == '0'
){
if(isset($result[$arr[$key_sortby]])){
$func_error_on_duplicate_key(
'duplicate sorting key given at target `'.$arr[$key_extend_target].'`!'.
' (key `'.$key_sortby.'`, value `'.$arr[$key_sortby].'`)'
);
}else{
$result[$arr[$key_sortby]] = $arr;
}
}else{
$tomerge[] = $arr;
}
unset($source[$key]);
}
$chk = array();
while(!empty($tomerge)){
foreach($tomerge as $key => $arr){
if($func_rec_sort($result,$arr,$para,$func_rec_sort,$func_error_on_duplicate_key)){
unset($tomerge[$key]);
}else{
$chk[$key]['unassigned'] = (isset($chk[$key]['unassigned']))
?++$chk[$key]['unassigned']
:1;
if($chk[$key]['unassigned'] >= $n-1){
if($unassigned !== false){
$unassigned[] = $tomerge[$key];
}
unset($tomerge[$key]);
}
}
}
ksort($result);
}
return $result;
}
Alles anzeigen