以csv格式读取类别和子类别并准备多维数组

时间:2016-12-28 14:49:46

标签: php arrays csv multidimensional-array

在PHP中,我需要以下面的数组格式转换下面的图像CSV文件:

此代码用于创建类别和子类别。如果客户将添加额外的类别和子类别,则需要按此工作。所以我需要动态的。

enter image description here

数组格式:

Array
(
    [0] => Cateory1
    [Cateory1] => Array
        (
            [0] => SubCategory11
            [1] => SubCategory12
            [2] => SubCategory13
            [3] => SubCategory14
        )

    [1] => Cateory2
    [Cateory2] => Array
        (
            [0] => SubCategory21
            [1] => SubCategory22
            [2] => SubCategory23
                [SubCategory23] => Array
                (
                    [0] => SubCategory221
                    [1] => SubCategory222
                )
            [3] => SubCategory24
        )

    [2] => Cateory3
    [Cateory3] => Array
        (
            [0] => SubCategory31
            [1] => SubCategory32
            [2] => SubCategory33
        )

    [3] => Cateory4
    [Cateory4] => Array
        (
            [0] => SubCategory41
            [SubCategory41] => Array
            (
                [0] => SubCategory411
                [SubCategory411] => Array
                (
                    [0] => SubCategory4111
                    [SubCategory4111] => Array
                    (
                        [0] => SubCategory41111
                    )
                )
            )
        )
)

请帮助我实现这一目标。

感谢。

1 个答案:

答案 0 :(得分:1)

虽然我同意Pitchinnate,但我有一些空余时间,我讨厌解析那种csv垃圾,所以我想我试一试。这是一段可能让你前进的代码。我没有测试或优化任何东西,所以如果这不能按预期工作,它可能仍然指向正确的方向。

// I assume, $imput is a twodimensional array containing the csv data

// the multi dimensional result array
$result = array();

// accumulated categories of current-1 line
$lastCat = array();
foreach($input as $line) {
    // accumulated categories of current line
    $cat = array();



    //First we create an array in $cat that contains the full "breadcrumbs" per line e.g. 
    // ['category1', 'SubCategory11', 'SubCategory114', ...]
    $marker = PHP_INT_MAX;
    for ($i=0; $i<count($line); $i++) {
        if ($marker < $i) {
            $cat[$i] = '';
        } else if ($line[$i] != '') {
            $cat[$i] = $line[$i];
            $marker = $i;
        }
    }

    // then using these category breadcrumbs, we create the result arrays
    // you could do this using recursion if you want
    $tmp = $result;
    for ($i=0; $i<count($cat); $i++) {
        // if we haven't seen this category yet, create it
        // a bit bulky, but necessary as you want the categories with numeric indices as well
        if (!isset($lastCat[$i]) || $cat[$i] != $lastCat[]) {
            $tmp[] = $cat[$i];
            // Check if there are still subcategories left in this line...
            if (!empty($cat[$i+1])) {
                //... and if so, create the category named index if not already existing
                if (!array_key_exists($cat[$i], $tmp)) {
                    $tmp[$cat[$i]] = array();
                } 
                $tmp = $tmp[$cat[$i]];
            } else {
                // ... and if not, we are finished with this line
                break;
            }
        }
    }
    $lastCat = $cat;
}