douangoo48863
2016-06-02 15:37
浏览 60
已采纳

在PHP中用树中的逗号分隔值转换字符串

I have a csv file that contains a field with a category tree in a comma separated format like this:

MotherCategory, ChildCategory1, ChildCategory2, ecc.

Some records contains only a category, other ones two, other ones three or more.

I would like to be able to store in my db all the unique categories and organise every record with a parentid scheme where the parentid value is the id of the category one level up.

At the end I should have something like:

id: 1, catname: MotherCategory, parentid: NULL
id: 2, catname: ChildCategory1, parentid: 1
id: 3, catname: ChildCategory2, parentid: 2

I've already filtered the data removing duplicates with array_unique(), next using explode() I am able to separate the values and I was counting the values and separating the groups basing on the length of the resultant arrays to build a tree, but I think that at the moment I am missing something to get my final result.

I've the following code. Can someone please give me a hint to solve the problem?

$cats = array_of_comma_separated_values;

foreach ($cats as $cat) {
    $catarray[] = explode(",",$cat);
}

//first level
$level1 = array_filter ($catarray, function($item) {
    if (count($item) == 1) { return true; } return false; 
});

//second level
$level2 = array_filter ($catarray, function($item) {
    if (count($item) == 2) { return true; } return false;
});

$level3 = array_filter ($catarray, function($item) { 
    if (count($item) == 3) { return true; } return false;
});

$level4 = array_filter ($catarray, function($item) {
    if (count($item) == 4) { return true; } return false;
});

$level5 = array_filter ($catarray, function($item) {
    if (count($item) == 5) { return true; } return false;
});

$level6 = array_filter ($catarray, function($item) {
    if (count($item) == 6) { return true; } return false;
});

$level7 = array_filter ($catarray, function($item) {
    if (count($item) == 7) { return true; } return false;
});

This gives me several arrays that i've to iterate to achieve what i'm looking for.

Now thanks to the suggestion from Kovlar I'm working on something based on array_pop() and array_replace_recurive().

I've edited the post because maybe I was not so clear.

1条回答 默认 最新

相关推荐 更多相似问题