将谷歌产品分类转换为多维数组

use*_*206 1 php arrays multidimensional-array

有没有一种简单的方法可以将谷歌产品分类列表转换为多维数组?

http://www.google.com/basepages/producttype/taxonomy.en-GB.txt

到目前为止我已经

    $taxonomy = file_get_contents('assets/taxonomy.en-GB.txt');
    $taxonomy = explode("\n", $taxonomy);
    array_shift($taxonomy);
Run Code Online (Sandbox Code Playgroud)

但这只是将其放入平面数组中。

小智 5

又快又脏:

<?php
$file = file_get_contents('taxonomy.data');
$file = explode("\n", $file);
$taxonomy = array();
foreach ($file as $index => $line) {
    $fields = explode(" > ", $line);
    $cursor = &$taxonomy;
    foreach ($fields as $field) {
        if (!isset($cursor[$field])) {
            $cursor[$field] = array();
        }
        $cursor = &$cursor[$field];
    }
}

print_r($taxonomy);
?>
Run Code Online (Sandbox Code Playgroud)