Pergunta

I have an array with rows and columns as well values for the corresponding rows and columns. Now I need to insert those values into my table.

The array which I have as below:

Array
(
     [cols] => Array
        (
            [0] => c1
            [1] => c2
            [2] => c3
        )

    [rows] => Array
        (
            [0] => R1
            [1] => R2
            [2] => R3
            [3] => R4
        )

    [name] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 1
            [4] => 2
            [5] => 3
            [6] => 1
            [7] => 2
            [8] => 3
            [9] => 1
            [10] => 2
            [11] => 3
        )

)

The table structure what I need is:

id | row | column | value
--------------------------
1  | r1  |  c1    |  1
2  | r1  |  c2    |  2
3  | r1  |  c3    |  3
4  | r2  |  c1    |  1
5  | r2  |  c2    |  2
6  | r2  |  c3    |  3
7  | r3  |  c1    |  1
8  | r3  |  c2    |  2
9  | r3  |  c3    |  3
etc..

If I try this in for and for each loop the looping continuously looping that number of times..

Please help me to rid out this problem

Foi útil?

Solução

You can try something like this:

$i = 0;
$table = array();

foreach ( $array['rows'] AS $row ) {
    foreach ( $array['cols'] AS $col ) {
        $table['id'] = $i + 1;
        $table['row'] = $row;
        $table['column'] = $col;
        $table['value'] = $array['name'][$i];
    }

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top