How to deduce all results from a cycle?

Example:

<?php for($i = 1; $i <= 10; $i++) { $data = array('ФИО1', 'ФИО2', 'ФИО3'); print_r($data); } print_r($data); print_r($data); в цикле выводит 10 раз $data print_r($data); за циклом выводит только последний результат $data 

I need to access the variable with all the data for further processing and saving to the excel file.

And if in a cycle to make record of an array in mysql and already from there to take data?

    3 answers 3

    I do not think I understood the question correctly, but if you want to save the data recorded in a cycle, then:

     $data = array(); for($i = 1;$ <=10; $i++){ array_push($data, 'ФИО1', 'ФИО2', 'ФИО3'); } print_r($data); 

    If you need a two-dimensional array, then:

     $data = array(); for($i = 1;$i <= 10;$i++){ $data[$i] = array('ФИО1', 'ФИО2', 'ФИО3'); } print_r($data); 

      It makes no sense to write in mysql, rather an array variable:

       $data = array(); for($i = 1; $i <= 10; $i++) { $data[] = array('ФИО1', 'ФИО2', 'ФИО3'); } print_r($data); 

        You can use a separate variable to store the entire result of the output in such a way that each iteration in this variable will increase the array (although there may be a string) of values.

        For example:

         $data = array(); $data_all = array(); for($i=1;$i <= 10; $i++) { $data = array('ФИО1', 'ФИО2', 'ФИО3'); print_r($data); array_push($data, ('ФИО1', 'ФИО2', 'ФИО3')); } 

        Check if you may need to add an array to the array. 🤔

        • Your array_push doesn't do what you expect. It is probably worth removing the brackets. - vp_arth