How to sort a complex PHP array -


i trying reorganize php array (on wordpress page). site coded else, , i'm trying make things better. think did things in confusing way (poor use of categories). rather recode entire site, wanted see if me work around fix.

so, basically, have array inside array. list of categories, , each category has specific details. i'm calling categories, based on it's parent. need reorganize list/array of categories, can print out in order 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 $category) {  ?>  <div> 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 1 page, $cat_tree outputs "entertainment:actors:".

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

$topcat[1] outputs "actors"

when "foreach" runs, list sorted id ...

  • actors (13)
  • artists (14)
  • directors (25)
  • writers (26)
  • deejays (35)

i need reorganize list specific order. each page different. may have reorganize list differently each page. may need...

  • writers (26)
  • actors (13)
  • deejays (35)
  • artists (14)
  • directors (25)

so, need way this. thought maybe "if" on "actor page", slide "writers" end of array (i know there way this), slide actors end, deejays, artists, directors. run foreach.

or, if page else, can slide other elements end of array. seems bit much, it's work around think of. of course, php knowledge limited, appreciated.

you try 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 need different order array.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -