doulian8485 2015-07-06 10:26
浏览 10

多维数组,没有重复的字符串

I am attempting to create a multi-dimentional array from a mutable strings using "." as a delimiter I have attempted to do explode using "." but this doesn’t give the outcome I am looking for

Input

file1
file1.file2
file1.file3
file1.file3.file4
file5
file5.file6

I am looking for an outcome of

array("file1" => array("0" => "file1", file1.file2 = array("0" => "File2"), array("file1.file3" => array("0" => "file3", "file1.file3.file4" => array("0" =>"file4")),"file5" => array("0"=>"file5", "file5.file6" => array("0" => "file6")));

if anyone is able to help I would be much appreciated

Cheers

Vip32

  • 写回答

1条回答 默认 最新

  • doulu3399 2015-07-06 13:53
    关注

    after much more searching I have been able to find some code that produces the result I was looking for. if anyone is interested it is as follows

    function AddFolder(array & $array, $string)
    {
        $path = explode(".", $string);
        $aux =& $array;
        foreach($path as $key)
        {
            $patkey[] .= $key;
            $pathkey = implode(".", $patkey);
            if(isset($aux[$pathkey]))
            {
                $aux =& $aux[$pathkey];
            }else{
                $aux[$pathkey] = ["0" => $key];
                $aux =& $aux[$pathkey];
            }
        }
    }
    
    $arraylist = array();
    
    foreach($list as $line)
    {
        AddFolder($arraylist, $line);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?