如何填充多维数组的笛卡尔积

时间:2017-05-21 01:07:43

标签: php arrays cartesian-product

我有一个这样的数组:

$string = array(
             "name" => "Mike",
             "country"=> "UK",
             "prefers" => array( "coffee", "milk", "bananas"),
             "age"=> "25"
             "children => array( Jack, Jill)
             );

如何使用所有参数填充“表格”,以获得如下所示的结构:

array1 = (Mike, UK, coffee, 25, Jack)
array2 = (Mike, UK, milk, 25, Jack)
array3 = (Mike, UK, bananas, 25, Jack)
array4 = (Mike, UK, coffee, 25, Jill)
array5 = (Mike, UK, milk, 25, Jill)
array6 = (Mike, UK, bananas, 25, Jill)

在这里,我需要一个1 * 1 * 3 * 1 * 2 = 6行和5列结构,没有空单元格。

4 个答案:

答案 0 :(得分:1)

我第一次搞砸了,因为我的解决方案并没有产生所有的排列。这是另一回事,这应该是一个正确的通用解决方案:

function arrayCrossProduct( array $data ) {
  $rowCount = 1; // keep track of total rows we need to generate

  // the reference is just used for convenience to cast to and reset the array
  foreach( $data as &$datum ) {
    $datum = (array) $datum; // cast to array
    $rowCount *= count( $datum ); // multiply total rows with current element count
    reset( $datum ); // reset the array, just to be sure
  }

  $result = array();
  while( $rowCount-- > 0 ) {
    $row = array();
    foreach( $data as &$datum ) {  // reference necessary to keep track of array pointer
      $row[] = current( $datum ); // generate new row
    }
    $result[] = $row; // add row to result

    // find the first element that is advanceable
    foreach( $data as &$datum ) {  // reference necessary to keep track of array pointer
      next( $datum );

      // if it advanced/not at the end: break
      if( !is_null( key( $datum ) ) ) {
        break;
      }

      reset( $datum ); // else rewind the array and continue
    }
  }

  return $result;
}

您可以查看working online,其中包含一些示例数据和一些格式化输出。

答案 1 :(得分:0)

你的问题很模糊,但如果我认为它是那么答案就是使用json_encode($string,TRUE);

答案 2 :(得分:0)

你在找这样的东西吗?

   <?php
  // Print the array in a nice form
  function print_r2 ($Array) {
    echo "<pre>";
    print_r($Array);
    echo "</pre>"; }


$array1[] = array(Mike, UK, coffee, 25, Jack);
$array1[] = array(Mike, UK, milk, 25, Jack);
$array1[] = array(Mike, UK, bananas, 25, Jack);
$array1[] = array(Mike, UK, coffee, 25, Jill);
$array1[] = array(Mike, UK, milk, 25, Jill);
$array1[] = array(Mike, UK, bananas, 25, Jill);




foreach($array1 as $array6){
    //first create multidimentional arrays to separate duplicates using values as keys.
if(!in_array($array6[2],$parray[$array6[0]][$array6[1]][$array6[3]][prefers])) {
                        $parray[$array6[0]][$array6[1]][$array6[3]][prefers][]=$array6[2];}

if(!in_array($array6[4],$parray[$array6[0]][$array6[1]][$array6[3]][children])) {
                        $parray[$array6[0]][$array6[1]][$array6[3]][children][]=$array6[4];}

}
print_r2($parray);

foreach($parray as $name => $value){
    $array[name] = $name;
        foreach($value as $country => $value2) {
            $array[country] = $country;
                foreach($value2 as $age => $value3  ){
                    $array[prefers] = $value2[$age][prefers];
                    $array[age] = $age;
                    $array[children] = $value2[$age][children];
                }

        }

}
        print_r2($array);
//OR
echo json_encode($array);
                            ?>

答案 3 :(得分:0)

我认为这更像是您在修改问题时所寻找的内容。我只是在原始答案的底部添加了相反的目标。

       <?php
  // Print the array in a nice form
  function print_r2 ($Array) {
    echo "<pre>";
    print_r($Array);
    echo "</pre>"; }


$array1[] = array(Mike, UK, coffee, 25, Jack);
$array1[] = array(Mike, UK, milk, 25, Jack);
$array1[] = array(Mike, UK, bananas, 25, Jack);
$array1[] = array(Mike, UK, coffee, 25, Jill);
$array1[] = array(Mike, UK, milk, 25, Jill);
$array1[] = array(Mike, UK, bananas, 25, Jill);




foreach($array1 as $array6){
    //first create multidimentional arrays to separate duplicates using values as keys.
if(!in_array($array6[2],$parray[$array6[0]][$array6[1]][$array6[3]][prefers])) {
                        $parray[$array6[0]][$array6[1]][$array6[3]][prefers][]=$array6[2];}

if(!in_array($array6[4],$parray[$array6[0]][$array6[1]][$array6[3]][children])) {
                        $parray[$array6[0]][$array6[1]][$array6[3]][children][]=$array6[4];}

}
print_r2($parray);

foreach($parray as $name => $value){
    $array[name] = $name;
        foreach($value as $country => $value2) {
            $array[country] = $country;
                foreach($value2 as $age => $value3  ){
                    $array[prefers] = $value2[$age][prefers];
                    $array[age] = $age;
                    $array[children] = $value2[$age][children];
                }

        }

}
        print_r2($array);
//OR
echo json_encode($array);

echo"<br />";echo"<br />";
//** now backwards**
echo"<br />";
echo"<br />";
$no=1;
    foreach($array[children] as $children ) {
foreach($array[prefers] as $prefers ) {

    print("array".$no." = (".$array[name].", ".$array[country].",".$prefers.",".$array[age].",".$children.")<br />\n");

    $no++;
}
}







                            ?>