0

I am trying to reorganize a PHP array (on a WordPress page). My site was coded by someone else, and I'm trying to make things better. I think they did things in a confusing way (poor use of the categories). Rather than recode the entire site, I wanted to see if someone could help me work around a fix.

So, basically, I have an array inside an array. A list of categories, and each category has specific details. I'm calling the categories, based on it's parent. I need to reorganize the list/array of categories, so they can print out in the order that I need them to.

$cat_tree = get_category_parents(wt_get_category_ID(), FALSE, ':', TRUE);
$top_cat = split(':',$cat_tree);
$parent = $top_cat[0];
$idObj = get_category_by_slug($parent); 

$args = array(
    'child_of'                 => $idObj->term_id,
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 0);
    $counter_c=1;

$categories = get_categories( $args );

if($counter_c == 1) $counter_c = '';
  foreach ($categories as $category) {

?>

<div> this is where each category array info gets printed... information goes:<br />
Name: <?=$category->cat_name ?> (<?=$category->cat_ID ?>)
</div>


<?php if($counter_c == '') $counter_c = 1; $counter_c++; }  // Reset Query
wp_reset_query(); 
?>  

On one page, $cat_tree outputs "entertainment:actors:".

$parent or $topcat[0] outputs "entertainment"

$topcat[1] outputs "actors"

When the "foreach" runs, I get an list sorted by the ID ...

  • Actors (13)
  • Artists (14)
  • Directors (25)
  • Writers (26)
  • DeeJays (35)

I need to reorganize this list into a specific order. Each page is different. So I may have to reorganize a list differently for each page. I may need...

  • Writers (26)
  • Actors (13)
  • DeeJays (35)
  • Artists (14)
  • Directors (25)

So, I need a way to do this. I thought that maybe I could say "if" on the "actor page", slide "Writers" to the end of the array (I know there is some way to do this), then slide Actors to the end, then DeeJays, then Artists, then Directors. Then run the foreach.

Or, if page is something else, I can slide other elements to the end of the array. That seems a bit much, but it's the only work around that I could think of. Of course, my php knowledge is limited, so any help is appreciated.

2
  • 1
    How about sample input array and list of clear sorting rules? Commented Mar 7, 2012 at 20:51
  • Well, since you you wouldn't be doing something like sorting alphabetically or numerically by the ID (like in your second example) I'm not sure there's much PHP can do to help. Sounds like a custom sort to me... Commented Mar 7, 2012 at 21:09

1 Answer 1

1

You could try a custom sort:

$order = array('Writers', 'Actors', 'DeeJays', 'Artists', 'Directors');

function catsort($a, $b) {
    global $order;
    $ak = array_search($a->cat_name, $order);
    $bk = array_search($b->cat_name, $order);
    if($ak === false) $ak = 0;
    if($bk === false) $bk = 0;
    return $ak - $bk;
}

usort($categories, 'catsort');

Each page would just need a different order array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.