Good day, wizard!

I am a beginner programming webmaster, and I would like to know and get a little idea about array(); and foreach(); .

There is the following code:

 $array = array(); foreach ($file[1] as $key => $value) { $array['file'][] = [ 'bla-bla-bla' => $file[1][$key], 'bla-bla-bla2' => $file[2][$key], ]; } 

So, could this code be explained in plain language?

Also the question: if you remove $array = array(); , it still works. Why? Or is it optional in my case? What is he doing at all? Creates an empty array or what?

PS: In the office. The websites have a lot of information, but unfortunately, I don’t understand anything there.

  • Thanks for the amendment :) - WebMaster
  • The question "what is this code?" - it's not a question. It is necessary to clarify what exactly you already understand about arrays and foreach functions and to form specific clear questions. - Al Mr
  • About arrays I know that this is a type or data structure in the form of a set of components, and foreach is responsible for iterating over arrays. If you remove $ array = array (); it still works. Why? - WebMaster
  • $array = array(); - this is just the initialization of an empty array. It's like writing $string = ''; if you remove this line, it will work, because in php it is not necessary to initiate variables before they begin filling, and if you remove this line, then $array initiated already inside foreach() - Al Mr
  • Thanks for the clear answer! Another question, how would you explain it? $ array ['file'] [] = - WebMaster

1 answer 1

Line 1. Initialization of the array, works without it, because php does not require initialization, but does it itself when used.

 $array = array();//инициализация, пхп ее не требует foreach ($file[1] as $key => $value)//перебираем все элементы элемента массива $file по ключу и значению, исходя из этого этот массив имеет такую структуру: Массив([1]=>Массив(...) [2]=>Массив(...)); { $array['file'][] = [//создается новый элемент-массив в ячейке "file" массива $array 'bla-bla-bla' => $file[1][$key], //пара ключ-значение 'bla-bla-bla2' => $file[2][$key], //пара ключ-значение ]; } 

I hope explained clearly.