English 中文(简体)
阵列 PHP 的排列
原标题:Permutation of Array PHP

我的问题如下:

$multidmimensional = array(

 [0] => array(
    [0] => 1, 
    [1] => 2, 
    [2] => 3
  );


  [1] => array(
   [0] => 5, 
   [1] => 6, 
   [2] => 7
  );

  ...

  [2] => array(
   [0] =>,4 
   [1] => 5, 
  );

);

我可以拥有一个或一个以上的阵列, 并举前面两个阵列为例:

我应该以下列方式改变它们:

15
16
17

25
26
27

36
37
38

如果我有这三种阵列, 我应该得到这样的结果:

154
164
174
155
165
175

254
264
274
255
265
275

364
374
384
365
375
385

I am having some problems to make an algorithm that would fix this problem. Can anyone help me? Thanks in advance.

问题回答

这是一个不错的大脑取笑问题。这里我所想出的东西,见 运行演示 ,用于测试和调整。

$multidimensional = array(
  0 => array(
    0 => 1,
    1 => 2,
    2 => 3,
  ),
  1 => array(
    0 => 5,
    1 => 6,
    2 => 7,
  ),
  2 => array(
    0 => 4,
    1 => 5,
  ),
); // just your input


$permutations = array();
$count = count($multidimensional);
for ($i = 0; $i < $count; $i++) {
  $temp = array_map("permute",array($permutations),array($multidimensional[$i]));
  $permutations = $temp[0];
}
print_r($permutations); // OUTPUT

function permute($base,$add) {
  $result = array();
  if (count($base) > 0) {
    foreach ($base AS $val1) {
      if (count($add) > 0) {
        foreach ($add AS $val2) {
          $result[] = $val1.$val2;
        }
      }
      else {
        $result = $base;
      }
    }
  }
  else {
    $result = $add;
  }
  return $result;
}

我现在不能测试它,但是这应该有效: (可能含有打字机)

function permute($arrays){
      if(count($arrays)<2) return $arrays[0];//TODO error on count == 0
      $array1 = array_shift($arrays);
      $array2 = array_shift($arrays);
      $results = array();
      foreach($array1 as $elementOfOne){
        foreach($array2 as $elementOfTwo){
          $results[] = $elemnetOfOne . $elementOfTwo;
        }
      }
      array_unshift($arrays, $results);
      return permute($arrays);
    }
$data = array
(
   1  => array(5, 6, 7),
   2  => array(9, 25, 14)

);

for($i=0; $i<=count(array_keys($data)); $i++) {
   for($j=1; $j<=2; $j++) {
      $values[$i][] = $data[$j][$i];
   }
}

for($i=0; $i<count($values); $i++) {
  shuffle($values[$i]);
}

$newData = array();
for($i=0; $i<3; $i++) {
   for($j=1; $j<=2; $j++) {
       $newData[$j][] = array_pop($values[$i]);
   }
}
print_r($newData);

"http://phpad.org/run/ 1257199458" rel="no follow" >Fiddle





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签