sKunKbad Posted December 2, 2009 Share Posted December 2, 2009 I have no idea where to start with this. I've got an array of items have an associative array containing a letter as the key, and a integer as value. The value is to represent the parent id, meaning that the item actually belongs inside an array of it's parent. So an example: <?php $i['1'] = array('A' => '0'); $i['2'] = array('B' => '0'); $i['3'] = array('C' => '1'); $i['4'] = array('D' => '1'); $i['5'] = array('E' => '2'); $i['6'] = array('F' => '2'); $i['7'] = array('G' => '3'); /* expecting the following: array ( 'A' => array ( 'C' => array ( 'G' ), 'D' ), 'B' => array ( 'E', 'F' ) ) */ Got any ideas for functions that will sort this way? Am I missing a simple way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/183701-sorting-by-parent-id-in-associative-array/ Share on other sites More sharing options...
sKunKbad Posted December 2, 2009 Author Share Posted December 2, 2009 I still need help, and think I might be very close. Any help would be appreciated: <?php $i['1'] = array('A' => '0'); $i['2'] = array('B' => '0'); $i['3'] = array('C' => '1'); $i['4'] = array('D' => '1'); $i['5'] = array('E' => '2'); $i['6'] = array('F' => '2'); $i['7'] = array('G' => '3'); foreach($i as $k => $v) { if(current($v) == '0') { $new[key($v)] = array(); } else { // this gives the name of the parent letter $x = key($i[current($v)]); // send parent letter and child letter to function $awc = array ( $x , key($v) ); array_walk_recursive(&$new, 'find_and_store', $awc); } } function find_and_store($h, $g, $awc) { if($g = $awc[0]) { $new[$g][$awc[1]] = array(); } } print_r($new); /* expecting the following: array ( 'A' => array ( 'C' => array ( 'G' ), 'D' ), 'B' => array ( 'E', 'F' ) ) */ Quote Link to comment https://forums.phpfreaks.com/topic/183701-sorting-by-parent-id-in-associative-array/#findComment-969594 Share on other sites More sharing options...
sKunKbad Posted December 2, 2009 Author Share Posted December 2, 2009 Solved my problem, but I'd like to make it fully recursive for infinite levels of parents/childs, and I'm too tired to keep working: <?php $i['1'] = array('A' => '0'); $i['2'] = array('B' => '0'); $i['3'] = array('C' => '1'); $i['4'] = array('D' => '1'); $i['5'] = array('E' => '2'); $i['6'] = array('F' => '2'); $i['7'] = array('G' => '3'); $i['8'] = array('H' => '3'); foreach($i as $k => $v) { // if top level if(current($v) == '0') { // just add as top level $new[key($v)] = array(); } else { // this gives the name of the parent letter $parent_letter = key($i[current($v)]); // if matching key in tier one foreach ($new as $z => $y) { if($parent_letter == $z) { $new[$z][key($v)] = array(); break; } else { foreach($y as $u => $w) { if($parent_letter == $u) { $new[$z][$parent_letter][key($v)] = array(); break; } } } } } } print_r($new); /* expecting the following: array ( 'A' => array ( 'C' => array ( 'G', 'H' ), 'D' ), 'B' => array ( 'E', 'F' ) ) */ Quote Link to comment https://forums.phpfreaks.com/topic/183701-sorting-by-parent-id-in-associative-array/#findComment-969628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.