categories =
products[parent=0],
entertainment[parent=products],
computers[parent=entertainment],
graphics cards[parent=computers]
I take it that the array is like this:
$categories = array
(
'products'=>array('parent'=>0),//poor little orphan
'entertainment'=>array('parent'=>'products'),
'computers'=>array('parent'=>'entertainment'),
'graphics cards'=>array('parent'=>'computers'),
)
here is a solution that I mixed up right now:
Solution 1
$categories = array
(
'computers'=>array('parent'=>'entertainment'),
'entertainment'=>array('parent'=>'products'),
'products'=>array('parent'=>0),
'graphics cards'=>array('parent'=>'computers')
);
function addparentfirst(&$original,$array,$name,$data){
if(isset($data['parent']) && isset($original[$data['parent']])){
$array = addparentfirst($original,$array,$data['parent'],$original[$data['parent']]);
}
$array[$name] = $data;
unset($original[$name]);
return $array;
}
foreach($categories as $key=>$value){//goes over each category only once, contrary to what it looks like
$sortedcategories = addparentfirst($categories,$sortedcategories,$key,$value);
}
$categories = $sortedcategories;//NOW IT'S SORTED
print_r($categories);
Solution 2
//It's interesting that it doesn't loop infinitely
//I like this solution the most
function addparentfirst(&$array,$key){
if(isset($array[$key]['parent']) && !empty($array[$key]['parent'])){
addparentfirst($array,$array[$key]['parent']);
}
$data = $array[$key];
unset($array[$key]);
$array[$key] = $data;
return $array;
}
foreach($categories as $key=>$value){
addparentfirst($categories,$key);
}
print_r($categories);
Solution 3
function sortArrayByArray($array,$orderArray) {
$ordered = array();
foreach($orderArray as $key) {
if(isset($array[$key])) {
$ordered[$key] = $array[$key];
unset($array[$key]);
}
}
return $ordered + $array;
}
//Usage:
$categories = sortArrayByArray($categories,array('products','entertainment','computers','graphics cards'));
print_r($categories);
as seen here
Solution 4
function get_childless_category_name($array){
foreach($array as $key=>$value){
if(isset($value['parent']) && !empty($value['parent'])){
unset($array[$value['parent']]);
}
}
$names = array_keys($array);//names of all the child free categories
return array_pop($names);//get the last one (there should only be one)
}
function parent_comes_first(&$array,$key){
if(isset($array[$key]['parent']) && !empty($array[$key]['parent'])){
$array = parent_comes_first($array,$array[$key]['parent']);
}
$data = $array[$key];
unset($array[$key]);
$array[$key] = $data;
return $array;
}
//Usage:
$childless = get_childless_category_name($categories);
parent_comes_first($categories,$childless);
print_r($categories);