In our blog we have already talked about arrays in PHP, especially its creation and behavior. The idea now is to show how to perform the processing of a PHP array using a very simple scan logic.


Let’s simulate using an array of five positions where we will extract the value of each position from a counter. To do this we will use a repeating structure and other resources available in the language:


$array = array(1, 2, 3, 4, 5);
$count = count($array);

for ($i = 0; $i < $count; $i++) {

echo $array[‘$i’];



The applied logic works like this: first, it creates an array with the desired values ​​using the array () function. Then, a variable is created with the number of total positions in the array, so the variable has in this case the value 5. For the processing of each position, a repetition structure “for” is created, starting with a counter in zero. Now comes the part that requires greater attention. Every array, by default, starts its position count at 0. Thus, the control variable also needs to boot with that value. As the array has 5 positions and starts its counter in 0, you must create a logical rule in which only values ​​below the total amount should be processed. Otherwise, when accessing a non-existent position, the code will display an error. So one must always indicate that the control variable needs to be lower (<) than the total size of the array. The access to the array the position will be given by the indication of the control variable ($ i).

For our example we have a processing logic that can be applied to demands from simple to more complex. This same type of processing can also be done using the foreach command, whose principle is pretty much the same of the repeating structure used, the difference is that the command uses fewer indicators and control variables, as can be seen in the example below:



<select name=”cities”>

$cities = array(‘Curitiba’,’Sao Paulo’,’Recife’,’Rio de Janeiro’);

            foreach($cities  as $values){

echo ‘<option>’.$values.'</option>’;




In the example we have an HTML element being created in real time, creating the option selections from an array that contains the names of some cities.

You might also like…

Scriptcase uses the MVC template?

Maybe you might have thought about this at some point, “Scriptcase uses the MVC templates?" Well, ...

How to implement A2F using Own Development in NodeJS in a Scriptcase application

A2F using Own Development in NodeJS A2F: In a previous article, we talked about Swivel as an extern...

Specialists or generalists, who gets a better advantage by using Scriptcase?

Hey there! Today  we will figure what are the ideal profiles for those who wish to take advantage o...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at Learn more about our Privacy Police.