- . . . : 4- .
* , , :
# 1 - - : ()
$array=json_decode('{
"Group1": {
"Blue": {
"Round": {
"One": [
"Lawrence",
"Anant",
"B."
],
"Two": [
"Erwin"
]
}
},
"Green": [
"Bryan",
"Mick"
]
},
"Group2": [
"Peter",
"Kris"
]
}', true);
function recurse($array,$path=''){
foreach($array as $k=>$v){
if(!is_array(current($v))){
echo ($path?"$path > ":''),"$k > (".implode(', ',$v).")\n";
}else{
recurse($v,($path?"$path > $k":$k));
}
}
}
recurse($array);
:
Group1 > Blue > Round > One > (Lawrence, Anant, B.)
Group1 > Blue > Round > Two > (Erwin)
Group1 > Green > (Bryan, Mick)
Group2 > (Peter, Kris)
# 2 - 4- : ()
function recurse($array,$path='',&$result=[]){
foreach($array as $k=>$v){
if(!is_array(current($v))){
$result[]=($path?"$path > ":'')."$k > (".implode(', ',$v).')';
}else{
recurse($v,($path?"$path > ":'').$k,$result);
}
}
return $result;
}
var_export(recurse($array));
:
array (
0 => 'Group1 > Blue > Round > One > (Lawrence, Anant, B.)',
1 => 'Group1 > Blue > Round > Two > (Erwin)',
2 => 'Group1 > Green > (Bryan, Mick)',
3 => 'Group2 > (Peter, Kris)',
)
:
, , - json ( / ):
: ()
$seperateArray = json_decode('[
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "One", "tier5": "Lawrence" },
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "One", "tier5": "Anant" },
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "One", "tier5": "B." },
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "Two", "tier5": "Erwin" },
{ "tier1": "Group1", "tier2": "Green", "tier3": "Bryan" },
{ "tier1": "Group1", "tier2": "Green", "tier3": "Mick" },
{ "tier1": "Group2", "tier2": "Peter" },
{ "tier1": "Group2", "tier2": "Kris" }]',true);
foreach($seperateArray as $row){
$last_val=current(array_splice($row,-1)); // extract last element, store as string
$results[implode(' > ',$row)][]=$last_val;
}
foreach($results as $k=>$v){
echo "$k > (",implode(', ',$v),")\n";
}
// same output as earlier methods