PHP 多维数组 - 使其工作


PHP Multidimensional Array - Making It Work

我想做的我不知道它的正确名称,所以如果我使用了不正确的术语,我深表歉意。

以下是我想要实现的目标的基本示例:

$arrayData = (4,5,6,7,8,9,10);
$public function setArray($colNum){
    $i = 0;
    while($i < count($arrayData){

在这里我不知道该怎么办。我希望$colNum设置新数组中的列数。所以

if $colNum = 4, then $arrayNew[$i][$i][$i][$arrayData[$i]

然后设置。 目前,我已经创建了多个函数,每个函数处理不同数量的列并且有效,但它的范围可以从 2 列到 30 列不等,我知道必须有一种方法可以更有效地做到这一点。

下面是一个 4 列表的工作示例(代码尚未优化)

public function parseTable($tableHeader,$isMoney,$colNum) {
    $tableData = $this->myArray;
    $count = 0;
    $placeHolder = 0;
    while($count < count($tableData)){
        // This hides the column headers
        if($count > $colNum - 1) {
            $col1Array[$placeHolder] = $tableData[$count];
            $col2Array[$placeHolder] = $tableData[$count + 1];
            $col3Array[$placeHolder] = $tableData[$count + 2];
            $col4Array[$placeHolder] = $tableData[$count + 3];
            $placeHolder ++;
        }
        $count = $count + $colNum;
    }
    // Remove the empty lines from the array
    $col1Array = array_values(array_filter($col1Array));
    $col2Array = array_values(array_filter($col2Array));
    $col3Array = array_values(array_filter($col3Array));
    $col4Array = array_values(array_filter($col4Array));
    // Put the 0 back in the array
    $col1Array = array_values(str_replace('~',0,$col1Array));
    $col2Array = array_values(str_replace('~',0,$col2Array));
    $col3Array = array_values(str_replace('~',0,$col3Array));
    $col4Array = array_values(str_replace('~',0,$col4Array));
    $count = 0;
    $tableHTML = "<table class='"table table-striped table-hover'"  data-toggle='"table'"
           data-search='"true'"
           data-show-columns='"true'"
           data-pagination='"true'"
        >
        <thead>
            <tr>
                <th>$tableHeader[0]</th>
                <th>$tableHeader[1]</th>
                <th>$tableHeader[2]</th>
                <th>$tableHeader[3]</th>
            </tr>
        </thead>
        <tbody>";
    while($count < count($col1Array)){
        // Replaces empty values with the text set when calling the function
        if($col1Array[$count] == ""){
            $col1Array[$count] = $emptyResult;
        }
        if($col2Array[$count] == ""){
            $col2Array[$count] = $emptyResult2;
        }
        if($col3Array[$count] == ""){
            $col3Array[$count] = $emptyResult3;
        }
        if($col4Array[$count] == ""){
            $col4Array[$count] = $emptyResult4;
        }
        // Converts data into money if specified in the function
        if($isMoney[0] == "Currency"){
            if($col1Array != '0'){$colOne =  money_format('%.2n', $col1Array[$count]);}
            else{$col1Array = "$0.00";}
        }else{$colOne = str_replace('0.00','0',$col1Array[$count]);}
        if($isMoney[1] == "Currency"){
            if($col2Array != '0'){$colTwo =  money_format('%.2n', $col2Array[$count]);}
            else{$colTwo = "$0.00";}
        }else{$colTwo = str_replace('0.00','0',$col2Array[$count]);}
        if($isMoney[2] == "Currency"){
            if($col3Array != '0'){$colThree =  money_format('%.2n', $col3Array[$count]);}
            else{$colThree = "$0.00";}
        }else{$colThree = str_replace('0.00','0',$col3Array[$count]);}
        if($isMoney[3] == "Currency"){
            if($col4Array != '0'){$colFour =  money_format('%.2n', $col4Array[$count]);}
            else{$colFour = "$0.00";}
        }else{$colFour = str_replace('0.00','0',$col4Array[$count]);}
        $tableHTML = $tableHTML . "
        <tr>
            <td>$colOne</td>
            <td>$colTwo</td>
            <td>$colThree</td>
            <td>$colFour</td>
        </tr>";
        $count++;
    }
    $tableHTML = $tableHTML . "
        </tbody>
    </table>";
    $this->tableHTML = $tableHTML;
    $this->col1Array = $col1Array;
    $this->col2Array = $col2Array;
    $this->col3Array = $col3Array;
    $this->col4Array = $col4Array;
    $this->totalRows = count($this->col1Array);
}

这是array_push的解决方案:

$arrayData = array(4,5,6,7,8,9,10);
$setArray = function($colNum) use($arrayData) {
    $new_array = array();
    for ($i = 0; $i < $colNum; $i++){
        $old_array = array();
        array_push($new_array, $old_array);
    }
    array_push($new_array, $arrayData);
    return $new_array;
};
$new_array = $setArray(5);
echo '<pre>';
die(var_dump($new_array));

我不确定我是否理解你想做什么。虽然看起来你需要使用递归函数。

对于示例:

$array = array();
create_multi_array($array, 10);
function create_multi_array(&$array, $dim){
    if($dim==0)
        return;
    create_multi_array($array[0], $dim-1);
}
echo "<pre>";
var_dump($array);

输出:

array(1) {
  [0]=>
  array(1) {
    [0]=>
    array(1) {
      [0]=>
      array(1) {
        [0]=>
        array(1) {
          [0]=>
          array(1) {
            [0]=>
            array(1) {
              [0]=>
              array(1) {
                [0]=>
                array(1) {
                  [0]=>
                  array(1) {
                    [0]=>
                    NULL
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

感谢大家的帮助!我有很多我正在处理的代码,并试图使其尽可能小以解决我遇到的问题。我知道这可能会令人困惑,但我不想发布所有内容,因为我知道这是一个小问题,可以轻松解决。我一直在寻找一种在数组中存储可变数量的数组的有效方法。我的直接解决方案是为将要存储的每个数组创建函数。但是,我想要一种方法来使 1 个函数可以在数组中动态存储尽可能多的数组。 这是解决方案,感谢@Sofiane Sadi的代码。

$arrayData = array(array(7,8,9),array(5,6,7),array(8,9,10));
$setArray = function($colNum) use($arrayData) {
$new_array = array();
for ($i = 0; $i < $colNum; $i++){
    $old_array = array();
}
array_push($new_array, $arrayData);
return $new_array;
};
$new_array = $setArray(2);
echo '<pre>';
die(var_dump($new_array));