dongshan4316 2010-04-19 21:06
浏览 9
已采纳

如何使用PHP从数组创建子串?

I have an array of data that looks like this:

2008, Honda, Accord, Used, Car

And I'm trying to figure a way to make a number of sub strings from each item in the array. For example, I would like to loop each item and create the following substrings:

2008
2008 Honda
2008 Accord
2008 Used
2008 Car
2008 Honda Accord
2008 Honda Used
2008 Honda Car
2008 Accord Honda
2008 Accord Used
2008 Accord Car
2008 Used Honda
2008 Used Accord
2008 Used Car
2008 Car Honda
2008 Car Accord
2008 Car Used
Honda
Honda 2008
Honda Accord
Honda Used
Honda Car
Honda 2008 Accord
Honda 2008 Used
etc ...

I need to make sure that there are no duplicates created and I need to prevent it from adding the same word twice (ex: Honda Honda OR 2008 Honda 2008 - i dont want that). Has anyone wrote anything like this or know where I can find a script that works the same way?

  • 写回答

3条回答 默认 最新

  • duanniubeng2265 2010-04-19 22:09
    关注

    How about this:

    <?php
    
    $vals = array('2008', 'Honda', 'Accord', 'Used', 'Car');
    
    function make_keyword_strings($array, $maxdepth = false, $prefix = '') {
      $ret = array();
      // if we are tracking depths, subtract one from the depth:
      $newdepth = $maxdepth === false ? false : $maxdepth-1;
      if ($newdepth < 0) return $ret; // we went over our depth
      foreach($array as $key => $value) {
        $temp = $array;
        unset($temp[$key]); // remove the current key
    
        // add our current value ($prefix.$value), and recursively call the function with
        // a new prefix.
        $ret = array_merge($ret, 
                 array(trim($prefix.$value)), 
                 (array) make_keyword_strings($temp, $newdepth, $prefix.$value.' '));
      }
      return $ret;
    }
    
    echo(implode("
    ",make_keyword_strings($vals,3)));
    

    ---- Resulting output ----

    2008
    2008 Honda
    2008 Honda Accord
    2008 Honda Used
    2008 Honda Car
    2008 Accord
    2008 Accord Honda
    2008 Accord Used
    2008 Accord Car
    2008 Used
    2008 Used Honda
    2008 Used Accord
    2008 Used Car
    2008 Car
    2008 Car Honda
    2008 Car Accord
    2008 Car Used
    Honda
    Honda 2008
    Honda 2008 Accord
    Honda 2008 Used
    Honda 2008 Car
    Honda Accord
    Honda Accord 2008
    Honda Accord Used
    Honda Accord Car
    Honda Used
    Honda Used 2008
    Honda Used Accord
    Honda Used Car
    Honda Car
    Honda Car 2008
    Honda Car Accord
    Honda Car Used
    Accord
    Accord 2008
    Accord 2008 Honda
    Accord 2008 Used
    Accord 2008 Car
    Accord Honda
    Accord Honda 2008
    Accord Honda Used
    Accord Honda Car
    Accord Used
    Accord Used 2008
    Accord Used Honda
    Accord Used Car
    Accord Car
    Accord Car 2008
    Accord Car Honda
    Accord Car Used
    Used
    Used 2008
    Used 2008 Honda
    Used 2008 Accord
    Used 2008 Car
    Used Honda
    Used Honda 2008
    Used Honda Accord
    Used Honda Car
    Used Accord
    Used Accord 2008
    Used Accord Honda
    Used Accord Car
    Used Car
    Used Car 2008
    Used Car Honda
    Used Car Accord
    Car
    Car 2008
    Car 2008 Honda
    Car 2008 Accord
    Car 2008 Used
    Car Honda
    Car Honda 2008
    Car Honda Accord
    Car Honda Used
    Car Accord
    Car Accord 2008
    Car Accord Honda
    Car Accord Used
    Car Used
    Car Used 2008
    Car Used Honda
    Car Used Accord
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效