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条回答 默认 最新

  • douyue1998 2016-06-02 16:08
    关注

    I suggest the use of array_replace_recursive() to simplify the tree-merging :)

    // $tree will contain the tree at the end of the script
    $tree = [];
    // we treat each row of the csv one by one
    foreach($csv_rows as $row) {
        // first, we split the string into an array
        $root_to_leaf = explode(',', $row['root_to_leaf']);
        // this is the "leaf" we want to append to the tree
        $leaf = [array_pop($root_to_leaf) => $row['value']];
        // we rebuild the path from the leaf to the root
        while(!empty($root_to_leaf)) {
            // add the next branching toward the root
            $leaf = [array_pop($root_to_leaf) => $leaf];
        }
        // we append the leaf to the tree
        $tree = array_replace_recursive($tree, $leaf);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么