1

By using the code below in CodeIgniter, we can generate a table as below

$this->load->library('table');

$data = array(
             array('11', '12', '13'),
             array('21', '22', '23'),
             array('31', '32', '33'),
             array('41', '42', '43')    
             );

echo $this->table->generate($data);

Output:
enter image description here

I want to ask how can I put this array in for loop likes:

for ($x = 0; $x < 5; $x++) {
    for ($y = 0; $y < 4; $y++) {
        $data xxx;
    }
}

What is the code to replace in xxx?

Thanks

2 Answers 2

1

Maybe:

$data= array();
for ($x = 1; $x < 5; $x++) {
    $data[$x]= array(); 
    for ($y = 1; $y < 4; $y++) {
        $data[$x][]= ($x*10)+($y);
    }
}
echo "<pre>";
print_r($data);
Sign up to request clarification or add additional context in comments.

2 Comments

well done, I misread the question. On my system the third line $data[$x]= array(); isn't required in order to work.
Can be omitted. PHP will create the array by assigning an element in $data[$x][]= ($x*10)+($y);
0

Here is how I would enumerate the array:

foreach ($data as $row) {
    foreach ($row as $cell) {
        echo $cell;
        // Do stuff here!
    }
}

If you need the index of each row / column you can use the following variant:

foreach ($data as $row_index => $row) {
    foreach ($row as $column_index => $cell) {
        // Stuff!
    }
}

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.