Jump to content

Sorting by parent id in associative array


sKunKbad

Recommended Posts

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?

 

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'
	)
)
*/

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'
	)
)
*/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.