1

我有一个非常金发的时刻,请原谅我......

我正在使用 PHP。

因此,在文本文档中,我有很多这样的内容:

Animals
Animals / Farm
Animals / Farm / Chickens
Animals / Farm / Chickens / Hens
Animals / Farm / Chickens / Roosters
Animals / Farm / Cows
Animals / Farm / Cows / Bulls
Animals / Farm / Donkeys & Mules
Animals / Farm / Ducks
Animals / Farm / Geese
Animals / Farm / Goats
Animals / Farm / Horses
Animals / Farm / Horses / Draft Horses
Animals / Farm / Horses / Draft Horses / American Cream Draft
Animals / Farm / Horses / Draft Horses / Belgian Draft Horse
Animals / Farm / Horses / Draft Horses / Clydesdale
Animals / Farm / Horses / Draft Horses / Percheron
Animals / Farm / Horses / Draft Horses / Shire Horse
Animals / Farm / Horses / Draft Horses / Spotted Draft Horse
Animals / Farm / Horses / Gaited Horses
Animals / Farm / Horses / Gaited Horses / American Saddlebred
Animals / Farm / Horses / Gaited Horses / McCurdy Plantation Horses
Animals / Farm / Horses / Gaited Horses / Missouri Fox Trotting
Animals / Farm / Horses / Gaited Horses / Paso Fino
Animals / Farm / Horses / Gaited Horses / Peruvian Paso
Animals / Farm / Horses / Gaited Horses / Rocky Mountain
Animals / Farm / Horses / Gaited Horses / Spotted Saddle Horse
Animals / Farm / Horses / Gaited Horses / Tennessee Walking
Animals / Farm / Horses / Ponies
Animals / Farm / Horses / Ponies / Australian Riding Pony
Animals / Farm / Horses / Ponies / Chincoteague
Animals / Farm / Horses / Ponies / Connemara
Animals / Farm / Horses / Ponies / Dales
Animals / Farm / Horses / Ponies / Dartmoor
Animals / Farm / Horses / Ponies / Exmoor
Animals / Farm / Horses / Ponies / Fell
Animals / Farm / Horses / Ponies / Haflinger
Animals / Farm / Horses / Ponies / Icelandic
Animals / Farm / Horses / Ponies / New Forest
Animals / Farm / Horses / Ponies / Quarter
Animals / Farm / Horses / Ponies / Shetland
Animals / Farm / Horses / Ponies / Sport Ponies
Animals / Farm / Horses / Ponies / Welsh Pony

我需要将它放入一个多维数组中,以便我可以从中构建一个多级菜单。我引入数据没有问题,问题在于解析。我在匹配换行符时遇到了麻烦,而且,假设我把它炸开了。我如何获得一堆数组,例如:

array(
    0=>Animals
    1=>Farm
    2=>Horses
    3=>Ponies
    4=>Welsh Ponies);

进入我需要的东西?.....看起来很简单,我遇到了一个主要问题......

请在正确的方向上提供帮助?

4

1 回答 1

2

Assuming it's PHP ;), here are some building blocks:

  1. Split a multiline text: https://stackoverflow.com/a/3997362/1665128

  2. For each line, explode it into a temporary array using ' / ' as a delimiter: http://php.net/manual/en/function.explode.php

  3. Convert the temp array into a map: http://php.bigresource.com/convert-a-simple-array-to-associative-array--lZQwGgRfV.html and the original How to convert a simple array to an associative array?

  4. Add the map to your multidimensional array: http://webcheatsheet.com/PHP/multidimensional_arrays.php

于 2012-12-26T17:16:24.940 回答