Today in the forum to see a friend's needs, witness CSDN moderators are worthy of Daniel's role exists AH!!! Save it for later use when needed.
String is:
1 = Photoelectric mouse
2 = mechanical mouse
3 = no mouse
1.1 = Black Photoelectric mouse
1.2 = Red Photoelectric mouse
1.2.1 = bluetooth Red optical Mouse
Moderator answers for:
<?php
$s =<<< TXT
1 = Optical Mouse
2 = mechanical mouse
3 = no mouse
1.1 = Black Photoelectric mouse
1.2 = Red Photoelectric mouse
1.2.1 = Bluetooth Red optical mouse
TXT;
$res = Array ();
foreach (Preg_split ("/[\r\n]+/", $s) as $r) {
list ($k, $txt) = explode (' = ', $r);
$p =& $res;
foreach (Explode ('. ', $k) as $v) {
if (! isset ($p [$v])) $p [$v] = array (' txt ' => $txt, ' child ' => Array ());
$p =& $p [$v] [' child ']
;
}
Print_r ($res);
? >
The printed array is:
Array ([1] => array ([txt] => optical mouse [child] => array
([1] => Array ([txt] => black Optical Mouse
[Child] => Array ()
) [2] => Array (
[TXT] => red optical Mouse [child] => Array
([1] => Array (
[txt] => bluetooth red Optical Mouse [child] => Array
(
) )
)
)
)
) [2] => array ([TXT] => mechanical mouse [child] => array
()) [3] => Array ([txt] => no mouse [Child] => Array ())
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/