0

What I am trying to do I do not know the proper name for it so I apologize if I have used the incorrect terminology.

Here is a basic example of what I am trying to achieve:

$arrayData = (4,5,6,7,8,9,10);

$public function setArray($colNum){
    $i = 0;
    while($i < count($arrayData){

Here is where I don't know what to do. I want $colNum to set the number of columns in the new array. so

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

would then be set. Currently I have created multiple functions that each handle a different number of columns and that works, but it can range from 2 columns to 30 columns and I know there has to be a way to do it more efficiently.

Here's a working example of a 4 column Table (Code not optimized yet)

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);
}
4
  • 1
    tried an recursive function that adds an array to itself'S child for n times? after that adding your data Commented Dec 11, 2014 at 15:01
  • I didn't even think about using a recursive function, but that with the array_push is a great solution to my problem. Commented Dec 11, 2014 at 15:11
  • I'm having trouble understanding what you want. Is $colNum really the number of columns? It looks like it's the number of dimensions. Commented Dec 11, 2014 at 15:25
  • $colNum is the number of dimensions. Each dimension is an array containing a table column's data, so it is technically the number of columns that the table has. There are calculations done on each columns data before data can be sent to the database. Else I would just break it all down and store it in a database first. Commented Dec 11, 2014 at 16:13

3 Answers 3

2

Here is a solution with 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));
Sign up to request clarification or add additional context in comments.

3 Comments

I'll give +1 for function ... use, didn't know about that, thanks!
This works great! Made a couple of modifications for my use. See my answer below for what I modified.
What's the point of using the anonymous function? Why not function setArray($colNum, $arrayData)? Or if you want to access the global variable, use global $arrayData.
1

I'm not sure I get what you are trying to do. Though it does look like you need to use a recursive function.

For the example :

$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);

outputs :

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 Comment

I am reading HTML tables and converting the columns into a CSV format. I am then exploding those into arrays. All of this ends up in a database after I am done with it. The issue is I have to create a different function based on the number of columns. A recursive function using array_push() should do the trick though with what I am trying to achieve. Thanks for your answer.
1

Thanks everyone for your help! I have a lot of code that I am working on and tried to make it as small as possible to get to the issue that I had. I know it can be confusing but I didn't want to post everything as I knew this was a small issue that with an easy fix. I was looking for an efficient way of storing a variable number of arrays in an array. My immediate solution was to create functions for each number of arrays that will be stored. But, I wanted a way to make it where 1 function could store as many arrays in an array dynamically. Here's the solution thanks to @Sofiane Sadi for the code.

$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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.