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.

arrays_php2

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:

<?php

$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>’;

}

</select>

 

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…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

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 sales@scriptcase.net. Learn more about our Privacy Police.