简化和抽象我的代码:组合字符串

发布于 2025-01-02 16:23:29 字数 951 浏览 0 评论 0原文

我想在 PHP 中组合字符串。我的脚本创建了所有可能的组合,如下所示。

$part1 = array('','d','n','s','g');
$part2 = array('a','e','o','oo');
$part3 = array('m','n','s','d','l','t','g','j','p');
$part4 = array('g','p','l','');
$part5 = array('g','p','l');
$part6 = array('a','e','o');
$part7 = array('d','l','r','');

$names = array();

foreach ($part1 as $letter1) {
    foreach ($part2 as $letter2) {
        foreach ($part3 as $letter3) {
            foreach ($part4 as $letter4) {
                foreach ($part5 as $letter5) {
                    foreach ($part6 as $letter6) {
                        foreach ($part7 as $letter7) {
                            $names[] = $letter1 . $letter2 . $letter3 . $letter4 . $letter5 . $letter6 . $letter7;
                        }
                    }
                }
            }
        }
    }
}

但我对我的解决方案并不满意。我的代码又快又脏。是否有一种解决方案可以使用灵活数量的零件数组,以便我可以轻松地通过例如 $part8 来扩展脚本? (不改变循环结构)

I want to combine strings in PHP. My script creates every possible combination like below.

$part1 = array('','d','n','s','g');
$part2 = array('a','e','o','oo');
$part3 = array('m','n','s','d','l','t','g','j','p');
$part4 = array('g','p','l','');
$part5 = array('g','p','l');
$part6 = array('a','e','o');
$part7 = array('d','l','r','');

$names = array();

foreach ($part1 as $letter1) {
    foreach ($part2 as $letter2) {
        foreach ($part3 as $letter3) {
            foreach ($part4 as $letter4) {
                foreach ($part5 as $letter5) {
                    foreach ($part6 as $letter6) {
                        foreach ($part7 as $letter7) {
                            $names[] = $letter1 . $letter2 . $letter3 . $letter4 . $letter5 . $letter6 . $letter7;
                        }
                    }
                }
            }
        }
    }
}

But I am not happy with my solution. I is quick and dirty code. Is there a solution wich works with a flexible number of part arrays, so I can extend the script by e.g. $part8 easiely? (without changing the loop construction)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2025-01-09 16:23:29

递归一:

function buildNames( $parts, $chars = ''){
  // Nothing to do, shouldn't happen
  if( !count( $parts)){
    return array();
  }

  $names = array();
  $part = array_shift( $parts);
  // Max level, we can build final names from characters
  if( !count( $parts)){
     foreach( $part as $char){
        $names[] = $chars . $char;
     }
     return $names;
  }

  // "We need to go deeper" and build one more level with remembered chars so far
  foreach( $part as $char){
     $names = array_merge( $names, buildNames( $parts, $chars . $char));
  }

  return $names;
}

$parts = array( $part1, $part2, $part3, $part4, $part5, $part6, $part7);
$names = buildNames( $parts);

从头开始,从头开始,评论如果有的话,但想法应该很好

Recursive one:

function buildNames( $parts, $chars = ''){
  // Nothing to do, shouldn't happen
  if( !count( $parts)){
    return array();
  }

  $names = array();
  $part = array_shift( $parts);
  // Max level, we can build final names from characters
  if( !count( $parts)){
     foreach( $part as $char){
        $names[] = $chars . $char;
     }
     return $names;
  }

  // "We need to go deeper" and build one more level with remembered chars so far
  foreach( $part as $char){
     $names = array_merge( $names, buildNames( $parts, $chars . $char));
  }

  return $names;
}

$parts = array( $part1, $part2, $part3, $part4, $part5, $part6, $part7);
$names = buildNames( $parts);

From head, from scratch, comment if something, but idea should be good

春花秋月 2025-01-09 16:23:29

您可以将此问题简化为六个笛卡尔积:

cartesianProduct($part1, 
  cartesianProduct($part2, 
    cartesianProduct($part3, 
      cartesianProduct($part4, 
        cartesianProduct($part5, 
          cartesianProduct($part6, $part7))))));


function cartesianProduct($p1, $p2) {
   $ret = array();
   foreach($p1 as $l1)
     foreach($p2 as $l2) 
        $ret[] = $l1 . $l2;
   return $ret;
}

You could reduce this problem to six cartesian products:

cartesianProduct($part1, 
  cartesianProduct($part2, 
    cartesianProduct($part3, 
      cartesianProduct($part4, 
        cartesianProduct($part5, 
          cartesianProduct($part6, $part7))))));


function cartesianProduct($p1, $p2) {
   $ret = array();
   foreach($p1 as $l1)
     foreach($p2 as $l2) 
        $ret[] = $l1 . $l2;
   return $ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文