构建递归函数以创建多维数组

时间:2013-02-03 12:37:33

标签: php arrays multidimensional-array

我想要的是一个递归函数,但请告诉我是否有另一种方法来实现我想做的事情!我不确定我会解释得这么好,所以如果你需要进一步澄清,请告诉我。

我有一个函数findSeed(),它接受一个id并返回一个包含“种子”信息的数组:

function findSeeds($id) {

  global $wpdb;

  $query = "SELECT * FROM seedTable WHERE id = " . $id;

  $seed = $wpdb->get_row($query, ARRAY_A);

  return $seed;

}

返回以下数组:

Array
(
    [id] => 9
    [name] => Sign
    [seed1] => 4
    [seed2] => 3
)

种子1和种子2是创造它所需的种子的ids。我正在寻找一个函数,它将创建一个多维数组,格式如下,其中数组的第一部分包含第一个“种子”,下一个包含第一个包含的两个种子,下一个包含两个种子各种各样的种子,等等:

Array
(    
    [1] => Array
        (
            [0] => Stripey Wallpaper
        )

    [2] => Array
        (
            [0] => Green Block
            [1] => Aqua Block
        )

    [3] => Array
        (
            [0] => Bricks
            [1] => Grass
            [2] => Bricks
            [3] => Glass Pane
        )

    [4] => Array
        (
            [0] => Rock
            [1] => Grass
            [2] => Dirt
            [3] => Rock
            [4] => Rock
            [5] => Grass
            [6] => Rock
            [7] => Lava
        )

)

到目前为止,我正在使用(可怕的)函数执行此操作,此处显示,但“地图”可能是2到8行之间的任何内容,我正在努力查看如何将其转换为整洁的小功能适用于任何事情:

function makeMap($id) {

    // First Row

    $rowNum = 1;

    $oneSeed = findSeeds($id);
    $map[$rowNum][] = $oneSeed[name];

    // Second Row

    $rowNum = $rowNum + 1;

    $twoSeed = findSeeds($oneSeed[seed1]);
    $threeSeed = findSeeds($oneSeed[seed2]); 

    $map[$rowNum][] = $twoSeed[name];        
    $map[$rowNum][] = $threeSeed[name];


    // Third Row

    $rowNum = $rowNum + 1;

    $fourSeed = findSeeds($twoSeed[seed1]);
    $fiveSeed = findSeeds($twoSeed[seed2]);
    $sixSeed = findSeeds($threeSeed[seed1]);
    $sevenSeed = findSeeds($threeSeed[seed2]);

    $map[$rowNum][] = $fourSeed[name];        
    $map[$rowNum][] = $fiveSeed[name];        
    $map[$rowNum][] = $sixSeed[name];        
    $map[$rowNum][] = $sevenSeed[name];

    // Fourth Row

    $rowNum = $rowNum + 1;

    $eightSeed = findSeeds($fourSeed[seed1]);
    $nineSeed = findSeeds($fourSeed[seed2]);
    $tenSeed = findSeeds($fiveSeed[seed1]);
    $elevenSeed = findSeeds($fiveSeed[seed2]);
    $twelveSeed = findSeeds($sixSeed[seed1]);
    $thirteenSeed = findSeeds($sixSeed[seed2]);
    $fourteenSeed = findSeeds($sevenSeed[seed1]);
    $fifteenSeed = findSeeds($sevenSeed[seed2]);

    $map[$rowNum][] = $eightSeed[name];        
    $map[$rowNum][] = $nineSeed[name];        
    $map[$rowNum][] = $tenSeed[name];        
    $map[$rowNum][] = $elevenSeed[name];
    $map[$rowNum][] = $twelveSeed[name];        
    $map[$rowNum][] = $thirteenSeed[name];        
    $map[$rowNum][] = $fourteenSeed[name];        
    $map[$rowNum][] = $fifteenSeed[name];

    return $map; 

}

当所有“行”节点都为空时它应该停止,因此以下只需要返回前两个“行”:

Array
(
    [1] => Array
        (
            [0] => Wood Block
         )

    [2] => Array
        (
            [0] => Dirt
            [1] => Lava
        )

    [3] => Array
        (
             [0] => 
             [1] => 
             [2] => 
             [3] => 
        )

    [4] => Array
        (
             [0] => 
             [1] => 
             [2] => 
             [3] => 
             [4] => 
             [5] => 
             [6] => 
             [7] => 
        )
)

任何提示,提示或解决方案都会非常感激,

谢谢。

编辑:快速说明我为什么喜欢上述格式 - 然后我将结果打印在表格中,第一个种子跨越整行,然后将其与下面的孩子分开,就像这样:

$map = makeMap($_POST['theItem']);

    $colspan = count(end(array_values($map)));      

    echo '<h3>' . $map[row1][0] . '</h3><br/>';

    echo '<table>';

    foreach ($map as $row) {

      echo '<tr>';

        foreach ( $row as $cell) {

          echo '<td colspan="'. $colspan .'"><div class="seed">' . $cell . '</div></td>'; 

        }

      $colspan = $colspan / 2;

      echo '</tr>';

    }

    echo '</table>';

2 个答案:

答案 0 :(得分:1)

<?php

$array = array('one', 'two', 'three');

for($i =1; $i<= count($array); $i++){
 for($j=0; $j<$i; $j++){
 $arrymulti[$i][$j]= $array[$j];
 }
}
echo '<pre>';
print_r($arrymulti);
?>

输出:

Array
(
    [1] => Array
        (
            [0] => one
        )

    [2] => Array
        (
            [0] => one
            [1] => two
        )

    [3] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

)

DEMO

答案 1 :(得分:1)

你可以使用我为类编写的递归函数

static public $map;
static function seeder($id,$Depth=0){
   $Seed=findSeeds($id);
   if(!$Seed){
        return self::$map;
   }
   self::$map[$Depth][]=$Seed['name'];
   self::seeder($Seed['seed1'],$Depth+1);
   self::seeder($Seed['seed2'],$Depth+1);
   if($Depth==0){
       foreach(self::$map as $k=>$v){
          $Flag=true;
          foreach($v as $key=>$Value){
              if($Value!="")$Flag=false;
          }
          if($Flag){
              for($i=$k;$i<=count(self::$map);$i++){
                  @unset(self::$map[$i]);
              }
              break;
          }
       }  
   }
   return self::$map;
}

示例

print_r(seeder($ID));

输出类似于您的样本