1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   | $array = array(
    0 => array(
		'id' => 1,
		'label' => 'Page 1',
		'parent' => 0,
		'current-item-parent' => 0,
		'active' => '0',
		'children' => array(
			0 => array( 
				'id' => 2, 
				'label' => 'Page 2',
				'parent' => 1,
				'current-item-parent' => 0,
				'active' => 0, 
				'children' => array(
					0 => array( 
						'id' => 3, 
						'label' => 'Page 3', 
						'parent' => 2,
						'current-item-parent' => 0,
						'active' => 0, 
						'children' => array() 
					),
					1 => array( 
						'id' => 4, 
						'label' => 'Page 4', 
						'parent' => 2,
						'current-item-parent' => 0,
						'active' => 1, 
						'children' => array() 
					)
				) 					
			)	
		)
	),
	2 => array(
		'id' => 5,
		'label' => 'Page 5',
		'parent' => 0,
		'current-item-parent' => 0,
		'active' => 0,
		'children' => array()
	)
);
 
function runArray($array) {
	foreach ( $array as $key => $node ) {
		if ( $node['active'] == 1 ) {
			update_CurrentItemParent_Key($array, $node['parent']);
			break; //Si possible en PHP
		}
		if ( !empty($node['children']) ) {
			runArray($node['children']);
		}
	}
 
	return $array;
}
 
function update_CurrentItemParent_Key($array, $currentNodeParentKey){
	$currentNodeIdKey = $currentNodeParentKey;
	$array[$currentNodeIdKey]['current-item-parent'] = 1;
	if ( $array[$currentNodeIdKey]['parent'] )
		update_CurrentItemParent_Key($array, $array[$currentNodeIdKey]['parent']);
 
	return $array;
}
 
echo '<pre>';
var_dump(runArray($array));
echo '</pre>'; | 
Partager