» PHP Recursive ksort: ksortTree

PHP Recursive functions

Yep, that's a tree alright.In real life I do not consider myself to be a tree-hugger, but as a developer: tree data structures and recursive technology totally get me going ;) Seriously though, they are very powerful ways to solve problems and often lead to elegant & reusable solutions.

In this series I am going to share a couple of functions and ways of approaching recursive problems in PHP.

Working with trees

When working with tree data structures you often need to craft them in different ways. PHP offers a lot of functions to change the shape of arrays, but often they only go 1 level deep. Trees can count an almost infinite number of levels. Hence we need recursive replacements for our beloved array functions.

ksortTree

ksortTree is the tree version of ksort. It will alphabetically reorder a tree based on it's keys.

<?php
/**
 * Recusive alternative to ksort
 *
 * @author  Kevin van Zonneveld <kevin@vanzonneveld.net>
 * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence
 * @version   SVN: Release: $Id: ksortTree.inc.php 223 2009-01-25 13:35:12Z kevin $
 * @link    http://kevin.vanzonneveld.net/
 *
 * @param array $array
 */
function ksortTree( &$array )
{
  if (!is_array($array)) {
    return false;
  }
 
  ksort($array);
  foreach ($array as $k=>$v) {
    ksortTree($array[$k]);
  }
  return true;
}

You probably shouldn't follow me


Like this Article?

I'd appreciate it if you leave a comment, spread the word, or consider a small donation


tags: php, programming, recursion, hierarchy, tree data structure, array
category: Programming - PHP - Tree Manipulation
read: 11,097 times

Add comment

(required, shown)(required, not shown)for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

 Track replies: rss feed comments feed

Comments

#8. Zetaphor on 18 July 2011

Gravatar.com: ZetaphorThis was exactly what I was looking for, got the job done beautifully. Thank you very much!

#7. Kevin on 21 February 2010

Twitter.com: kvz@ ds cartes: I miss an is_array check in your function, but yes. nice stuff.

#6. ds cartes on 02 February 2010

Gravatar.com: ds cartes

function natksort(&$array) {
$keys = array_keys($array);
natcasesort($keys);
 
foreach ($keys as $k) {
$new_array[$k] = $array[$k];
}
 
$array = $new_array;
return true;
}

like this ~

#5. eMPee584 on 05 December 2009

Gravatar.com: eMPee584thx! exactly what i needed to recursively sort that drupal view object before dumping it - niccce ;)

#4. Kevin on 25 January 2009

Twitter.com: kvz@ Zomb1e: Yo, thx!

#3. Zomb1e on 22 January 2009

Gravatar.com: Zomb1e

if (!is_array($array)) {
return true;


should be:
if (!is_array($array)) {
return false;

#2. Kevin on 03 November 2008

Twitter.com: kvz@ ZiTAL: You're welcome ;)

#1. ZiTAL on 29 October 2008

Gravatar.com: ZiTALIt's the milk!!! Thanl you very much :)