You are here: Re: Can't figure out how to create hierarchical multi array in such situation « PHP Programming Language « IT news, forums, messages
Re: Can't figure out how to create hierarchical multi array in such situation

Posted by Michael Fesser on 11/26/07 00:41

..oO(alexandis@gmail.com)

>I have the following multi array like this:
>[0] => [ [0] => "A", [1] => "B", [2] => "C" ]
>[1] => [ [0] => "A", [1] => "C", [2] => "D" ]
>[2] => [ [0] => "B", [1] => "A", [2] => "B" ]
>[3] => [ [0] => "B", [1] => "A", [2] => "D" ]
>
>It should be transformed into this:
>["A"] => [
> ["B"] => [
> "C"
> ],
> ["C"] => [
> "D"
> ]
> ],
>["B"] => [
> ["A"] => [
> "B", "D"
> ]
> ]
>
>- that is to say, i need to split the above structure into classes,
>subclasses, subsubclasses and so on, depth is variable...

<?php
$test = array(
array('A', 'B', 'C'),
array('A', 'C', 'D'),
array('B', 'A', 'B'),
array('B', 'A', 'D'),
);

$result = array();
foreach ($test as $array) {
$current = &$result;
foreach ($array as $item) {
$current = &$current[$item];
}
}

print_r($result);
?>

HTH
Micha

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация