PHP – Array loop

PHP – Array loop

 

Iterate array with foreach()

The main PHP built-in function used to iterate through array elements is foreach(). We learned about the foreach() loop in a dedicated tutorial.

Here is an example of how to loop through an array using foreach.

PHP

<?php
$file_list = array(
	"index" 	=> "home_page.php",
	"article" 	=> "article_page.php",
	"about" 	=> "about_page.php",
	"contact" 	=> "contact_page.php"
);

/*
 * Loop through the array and print all the values
 */
if(is_array($file_list)){
	foreach($file_list as $file){ // iterate files
		echo $file . "<br />";
	}
}
?>

This loop should print something like this:

PHP

home_page.php
article_page.php
about_page.php
contact_page.php

Using foreach like this will ignore the key name. But if you want to also get the key you must add it to the equation like this:

PHP

<?php

if(is_array($file_list)){
	foreach($file_list as $key=>$file){ // iterate files
		echo "'$file' is stored on key '$key'<br />";
	}
}
?>

This loop will give you the following result:

PHP

'home_page.php' is stored on key 'index'
'article_page.php' is stored on key 'article'
'about_page.php' is stored on key 'about'
'contact_page.php' is stored on key 'contact'

Iterate indexed arrays with count() and for()

You can also get all array values using the for() function. To do that you will need to previously get the total number of elements, that your array has. This is called the length of an array and we use count() to get it.

Imagine we have this following array. Here is how we get the length of it:

PHP

<?php
$templates = array(	"home", "article", "about", "contact" );

$length = count($templates);

echo "The array has $length elements.";  // The array has 4 elements.
?>

let’s now continue and iterate throw all the array values using for() function

PHP

<?php

$templates = array(	"home", "article", "about", "contact" );
$length = count($templates);

// build all file-names system using the $templates array
for($i=0; $i < $length; $i++) {
    echo $templates[$i] ."_page.php<br />";
}
?>

This will return the following result

PHP

home_page.php
article_page.php
about_page.php
contact_page.php

Debug – See all array values

Till now we have created our own array values, so you always know the values that we are working with. How about receiving an array from a function, database or another file and you have no idea what your array contains.

Let’s apply the print_r() on our first defined array. We used the pre tag to maintain the formatting.

PHP

<?php
echo "<pre>";
print_r($file_list);
echo "</pre>";
?>

This is how we obtain all the array elements. This is just beautiful 😉

PHP

Array
(
    [index] => home_page.php
    [article] => article_page.php
    [about] => about_page.php
    [contact] => contact_page.php
)

Another function that will display an array like this is var_dump(). var_dump will display structured information about the array that includes the type and value. Arrays are explored recursively with values indented to show structure. Depending on your needs you can use var_dump() or print_r().

PHPvar_dump($file_list);

Here is the output:

PHParray(4) { ["index"]=> string(13) "home_page.php" ["article"]=> string(16) "article_page.php" ["about"]=> string(14) "about_page.php" ["contact"]=> string(16) "contact_page.php" }

You can always apply the pre tag to it to add some formatting

PHP

<?php
echo "<pre>";
var_dump($file_list);
echo "</pre>";
?>

Here is the output:

PHP

array(4) {
  ["index"]=>
  string(13) "home_page.php"
  ["article"]=>
  string(16) "article_page.php"
  ["about"]=>
  string(14) "about_page.php"
  ["contact"]=>
  string(16) "contact_page.php"
}

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

 

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars