One more tip for those who are starting in php , continuing the previous tutorial. Let us understand how to sweep a multidimensional array with php
It is much simpler than in other languages. Which you would have to put two is like talking about … In php you have the foreach , which scans the array, and iterates automatically.
Running array
assuming a return of bank that has several arrays into one another, we access to all items of that array would do the following:
array ( 'Jackfruit' 'Melon', 'Watermelon' ) ); foreach ($ arr_result the $ data) { if (is_array ($ data)) { foreach ($ data as $ other_data) { echo $ other_data, '
'; } } else { echo $ date, '
'; } }
Explaining the code
The first lines are the definition of a array , so we can make the iterations.
The online foreach parameters are the array, and $ data is the variable that will receive the value of the iteration time.
The function is_array checks if the passed parameter is either not a array .
The function echo it displays on screen the value in the near variável.Nos chapters of this novel posts, I explain why to use a comma, not concatenation. Beforehand you advise to use this form.
Recursive Function
This code would be very efficient for an array that we know the definition of size, and a small sweep.
However for a huge array with many dimensions would be an immense source, so we can use recursion.
function recursive_show_array ($ arr) { foreach ($ arr the $ value) { if (is_array ($ value)) { recursive_show_array ($ value); } else { echo $ value; } } }
Thus if it is array, it will call the function again and do the loop “sub-array”, thus all levels will be achieved.
You can check the functions in the php documentation:
http://php.net/is_array
http://php.net/foreach
http://php.net/echo
Leave a Comment