@火箭Hazmat是正確的。破折號表示每個項目的深度。試試這個:
<?php
function parse_line($line)
{
$split = explode(" ", $line);
$result = count($split) == 1 ? array(0, $split[0]) : array(strlen($split[0]), $split[1]);
return $result;
}
function find_parent($item, $lines)
{
$dashes = str_pad("", $item[0], '-');
$orig_line = $item[0] ? $dashes.' '.$item[1] : $item[1];
$key_of_orig_line = array_search($orig_line, $lines);
$depth = $item[0];
for($i = $key_of_orig_line-1; $i > -1; $i--)
{
$current_item = parse_line($lines[$i]);
$current_depth = $current_item[0];
if($current_depth < $depth)
{
return $current_item;
}
}
return array(null, null);
}
function get_trail($item, $lines)
{
$trail = array();
$parent = find_parent($item, $lines);
$trail[] = $parent[1];
while($parent[1] != null)
{
$parent = find_parent($parent, $lines);
$trail[] = $parent[1];
}
return array_reverse($trail);
}
function add_to_menu($item, $trail, $menu)
{
$cursor = &$menu;
for($i = 0; $i < count($trail); $i++)
{
if($trail[$i] == null) continue;
$trail[$i] = substr($trail[$i], 0, -3);
if(!array_key_exists($trail[$i], $cursor) || $cursor[$trail[$i]] == '') $cursor[$trail[$i]] = array();
$cursor = &$cursor[$trail[$i]];
}
$cursor = array_merge($cursor, array(substr($item[1], 0, -3) => ''));
return $menu;
}
function assign_ids($lines)
{
$new = array();
$id_counter = 100;
foreach($lines as $line)
{
$new[] = $line.$id_counter;
$id_counter++;
}
return $new;
}
function p($s) { echo '<pre>'; print_r($s); echo '</pre>'; }
$input = "Company
- Apple
-- Inbound
-- Outbound
--- Delete
- Microsoft
-- Inbound
Invoices
- Customers
-- Apple
--- January";
$menu = array();
$lines = explode("\n", $input);
$lines = array_map("trim", $lines);
$lines = assign_ids($lines);
foreach($lines as $line)
{
$item = parse_line($line);
$trail = get_trail($item, $lines);
$menu = add_to_menu($item, $trail, $menu);
}
p($menu);
?>
你嘗試過什麼嗎?如果你發佈一些你已經嘗試過的代碼,你更有可能獲得幫助。 – Pitchinnate
嘗試遍歷每一行,並計算該行開始的'-'數。這會告訴你你的物品有多深,以及它是一個孩子,父母還是兄弟姐妹。 –
這是正確的,這就是我的嘗試,但我的問題是如何創建數組鍵...我試過strpos,str_repeat,preg_match_all ...但我從來沒有來到第三個鍵... – codeworxx