L
L
Luigi2021-05-14 17:33:22
PHP
Luigi, 2021-05-14 17:33:22

How to traverse two arrays at the same time?

I am new to PHP. I am doing my import/export of Excel data. I have a nested array with data like this:

Array
(
    [0] => Array
        (
            [number] => 1
            [full_name] => Иванов Иван Иванович
            [date] => 1995:07:02
            [adress] => г. Москва
            [passport] => ВС212928
            [childs] => да
            [salary] => 15,000.00 ₽
        )

    [1] => Array
        (
            [number] => 2
            [full_name] => Петров Петр Петрович
            [date] => 1993:01:01
            [adress] => г. Москва
            [passport] => АС212121
            [childs] => нет
            [salary] => 20,000.00 ₽
        )

    [2] => Array
        (
            [number] => 3
            [full_name] => Сидоров Николай Николеевич
            [date] => 1994:02:05
            [adress] => г. Москва
            [passport] => ВК199894
            [childs] => да
            [salary] => 1,200,000.00 ₽
        )
}


I insert this data into Excel, but now it works too hard, with reference to the number of rows in the table. Like this

foreach ($rows as $row) {
         $active_sheet->setCellValue('A' . $count, $row['number']);
         $active_sheet->setCellValue('B' . $count, $row['full_name']);
         $active_sheet->setCellValue('C' . $count, $row['date']);
         $active_sheet->setCellValue('D' . $count, $row['adress']);
         $active_sheet->setCellValue('E' . $count, $row['passport']);
         $active_sheet->setCellValue('F' . $count, $row['childs']);
         $active_sheet->setCellValue('G' . $count, $row['salary']);

         $count = $count + 1;
     }


I have created an array with an alphabet How can I dynamically substitute the alphabet letters and values ​​from my data array into the setCellValue() method I am trying to do this but it doesn't work because I have nesting
$alphabet = range('A','Z')



foreach ($rows as $key => $row) {
         $active_sheet->setCellValue($alphabet[$key] . $count, $row[$key]);
         $count = $count + 1;
     }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question