D
D
Dmitry2017-07-21 11:43:04
PHP
Dmitry, 2017-07-21 11:43:04

Construct a tree from a string?

Tell me, there is a line "max_rsya_new_hfequncy_platya_1priority", how to build a tree from this line?
Need this:
max
-- rsya
---- new
------ hfequncy
-------- platya
---------- 1priority

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2017-07-21
@dima_yakimneko

And one more option:

$input = explode('_', 'max_rsya_new_hfequncy_platya_1priority');
array_walk($input, function($str, $i){
    printf("%'-".($i*2+strlen($str))."s<br>\n", $str);
});
sandbox.onlinephpfunctions.com/code/24a9c7308f4e27...

A
Alexander Shapoval, 2017-07-21
@sanek_os9

$str = 'max_rsya_new_hfequncy_platya_1priority';
$array = explode('_', $str);
$new_str = $delimiter = '';
for ($i = 0; $i < sizeof($array); $i++) {
    $new_str .= $delimiter . $array[$i] . '<br />';
    $delimiter .= '--';
}
echo $new_str;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question