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:

<?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.

Check out more content on our blog!
Learn all about Scriptcase.

By ,

June 3, 2015

a

You might also like…

AI-Driven Software Development: The Role of ChatGPT

In the rapidly evolving world of technology, artificial intelligence (AI) is playing an increasingl...

Low-Code: The Key to Accessible Digital Transformation

Digital transformation is imperative for companies wishing to remain competitive in the current era...

Low-code: Simplifying Development Without Sacrificing Quality

In today's agile and competitive world, productivity is crucial for the success of any business. Th...

You might also like…

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.