PHP – Arrays

PHP – Arrays


An array is a type of data structure that allows you to store multiple values inside of a variable, on a key-value basis. Here is a simple example of an array in PHP.


$product = array("milk", "bread", "potato");
echo "Please buy " . $product[0] . ", " . $product[1] . " and " . $product[2] . " from the store.";

This is a good substitute for storing every product in his own variable. Imagine you had a few hundreds of products and you have to list them all:


$product_01 = "product_01";
$product_02 = "product_02";
$product_03 = "product_03";
$product_99 = "product_99";

This is not a good way to do it. Here is how you achieve the same thing using an array. An array in PHP allows you to access any value by referring it by his index or key.

Array types and how to create an array in PHP

To better understand arrays, imagine them as containers that you can use to store data. PHP has three different types of arrays or containers.

  • indexed arrays – this type of array is using numbers for their indexes or keys.
  • associative arrays – this type of arrays is using strings for their indexes or keys.
  • multidimensional arrays – arrays that contain various other arrays on one or more of their key positions


To create a simple array with no data in PHP, you can call the built in array() function.


$myArray = array();

Now that you created an array container, let’s go and put some values inside it.

Create PHP indexed array

Indexed arrays are the most simple type of array.


$products = array("product_1", "product_2", "product_3");

On the other hand, you can manually assign the index that an array will use to store a specific value.


$products[0] = "product_1";
$products[1] = "product_2";
$products[2] = "product_3";

Please bear in mind that if no index number is specified then the index values always start from 0.

The first and the second method of creating an array are both equivalents. But let’s complicate a little bit this and imagine that for some reason you want specific index for a specific value. Like for example, you want to assign your product name on the product ID value and that you product IDs are not consecutive. Here is how you can accomplish that


$products_array = array(22 => "product_1", 24 => "product_2", 102 => "product_3");

// this is equivalent
$products_array[22] = "better_product";
$products_array[24] = "cool_product";
$products_array[102] = "awesome_product";


Create PHP associative array

PHP associative arrays are created just the same way as indexed arrays. Now imagine you want to create a container or an array that contains all product details. Here is an example to illustrate that:


$products = array(
	"product_ref" => "15456645",
	"product_name" => "Awesome White Milk",
	"product_color" => "white"

// this is also equivalent
$products_array["product_ref"] = "15456645";
$products_array["product_name"] = "Awesome White Milk";
$products_array["product_color"] = "white";

Create PHP multidimensional arrays

Now think about you want to create an array that contains all products and all the product details. To do that you will need a multidimensional array or an array of arrays. You can have as many arrays inside arrays as you like.


$products = array(
	array( product_1_data ),
	array( product_2_data ),
	array( product_3_data )


This is a very simplified version of a multidimensional array. We will learn more about PHP multidimensional arrays in a future tutorial.


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