doujiexi1824 2016-06-23 17:21
浏览 31
已采纳

从字符串到特定组合的高效方式[关闭]

So I have a phrase like this:

one_two_three_four

.. and for clarity, it could be something like this too:

one_two_three_four_five_six_seven_eight_nine_ten_eleven_twelve

.. but for purposes of brevity, I am just going to use this one as an example:

one_two_three_four

From the string, I want to create the following keywords

one
one_two
one_two_three
two
two_three
two_three_four
three
three_four
four

I am looking for the most efficient way (in PHP) to parse out strings like this (some will be much larger).

I can get this far:

$keyword = explode('_', $string);

So now I have an array like this:

one
two
three
four

I am stuck on how to get from the original string into the variants.

  • 写回答

1条回答 默认 最新

  • duan2428 2016-06-23 17:43
    关注

    First thing you need to explode the string by _

    $str = 'one_two_three_four_five_six';
    $array = explode('_', $str);
    

    Add an empty array to store result there

    $result = [];
    

    Define a recursive function that takes an array, implode array values, remove last element and recall the same array until length is 0

    function visitArray($array, &$result) {
        if(count($array) == 0) //Check if length is 0 (Stop executing)
            return;
        $result[] = implode('_', $array); //Implode array values
        return visitArray(array_slice($array, 0, count($array) -  1), $result); //Remove last element and call again the same function
    }
    

    So if you pass [1, 2, 3] to visitArray you will have 1_2_3, 1_2, 1 in the result array.

    Now you need a secondary function to call visitArray with a new position,

    That means, if we have this array [1, 2, 3]

    we need to call visitArray [1,2,3], [2,3], [3]

    So we define a function with simple for loop to iterate around array values, and each time we call visitArray() and we ignore the called once by using array_slice with position variable.

    function callVisit($array, &$result, $position = 0) {
        for($i = 0; $i < count($array); $i++)
            visitArray(array_slice($array, $position++, count($array) - 1), $result);
    }
    

    Update: If you need to remove the position param you could replace the forloop with list as below:

    function callVisit($array, &$result) {
        while (list(,$v) = each($array)) 
        { 
            visitArray($array, $result);
            array_shift($array); //Remove first element
        } 
    }
    

    So you need to call callVisit() by passing two params, the array, result array (where result should be stored)

    callVisit($array, $result);
    

    Full code:

    <?php
    
    $str = 'one_two_three_four_five_six';
    
    $array = explode('_', $str);
    
    $result = [];
    
    
    function callVisit($array, &$result, $position = 0) {
        for($i = 0; $i < count($array); $i++) 
            visitArray(array_slice($array, $position++, count($array) - 1), $result);
    }
    
    function visitArray($array, &$result) {
        if(count($array) == 0)
            return;
        $result[] = implode('_', $array);
        return visitArray(array_slice($array, 0, count($array) -  1), $result);
    }
    
    
    callVisit($array, $result);
    
    echo "<pre>", json_encode($result, JSON_PRETTY_PRINT), "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line