en

PHP Arrays: how to use them in a project

October 19, 2022

Tags: Technologies

php

 

To design and create the websites and mobile applications that we visit daily, it is necessary to use programming languages ​​and one of the most popular is PHP, a language used by millions of experts worldwide.

 

PHP is a fast and secure language. PHP uses its own memory and competes well on speed, especially when using newer versions. There have been questions in the past about the security of PHP, although it is important to note that it is not inherently more or less secure than other programming languages.

 

A major benefit is that due to its widespread use and community support, there are now many tools, frameworks, and best practices to help fix vulnerabilities and protect against cyberattacks.

 

php array

 

PHP arrays

 

An array can be defined as a special variable that is used to store more than one value within a single variable without having to create more of these variables to store all the values.

 

To create an array in PHP, we use the array() array function.

 

As expected, there are different types of arrays in PHP:

 

  • Numeric arrays
  • Associative arrays
  • Multidimensional Arrays

 

Let's see how each of them works:

 

Numeric arrays

 

This is a type of matrix or array that can contain strings, numbers, and objects. Example:

 

<?php
// Numeric/ index arrays
$cars = array('Mecedes Benz', 'Hilux', 'Highlander', 'Hummer', 'Limozien');
var_dump($cars);
?>

 

Associative arrays

 

In a Freecodecamp article, they explain “An associative array is a type of array where the key has its own value. In an associative array, we make use of key and value. The keys are descriptive legends of the element of the array that are used to access the value of the array. And the value is the value assigned to the array element.

 

An example of an associative array would be:

 

<?php
$student_age = array (
'Scott_Mcall' => 17,
'Stalensky' => 18,
'Lydia' => 16,
'Allision' => 17,
);

echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17
echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18
echo $student_age ['Lydia']; //this code will display the age of Lydia as 16
echo $student_age ['Allision']; //this code will display the age of Allision as 17
?>

 

Multidimensional array

 

It can be defined as an array within an array. Each element of this array contains a sub-array within it and allows the PHP developer to store multiple arrays within a single variable.

 

For example, if you want to store the registration numbers, names and emails of part of a company's staff, multidimensional arrays would be used to achieve this. For example:

 

<?php
$Staffs = [
[
'Name' => 'Derek Emmanuel',
'Reg_No' => 'FE/30304',
'Email' => 'derekemmanuel@gmail.com'
],
[
'Name' => 'Rubecca Michaelson',
'Reg_No' => 'FE/20003',
'Email' => 'rmichealsongmail.com'
],
[
'Name' => 'Frank Castle',
'Reg_No' => 'FE/10002',
'Email' => 'fcastle86@gmail.com'
]
];
echo $Staffs[2] ['Email']; // This displays the email of the last staff which is fcastle86@gmail.com

echo $staffs [0] ['Name']; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel

// you can access the information of any staff you wish to by using echo $(variable name) [index number] ['array element key'].

?>

 

php array

 

Basic PHP Syntax

 

A PHP script can be placed anywhere in a document. This script starts with <?php and ends with ?>. We present an example:

 

<?php
// PHP code goes here
?>

 

The default extension of PHP files is: .php. A PHP file usually contains HTML tags and some PHP script code.

 

The following is an example of a simple PHP file, with a script that uses a built-in echo function to output the text "Hello world!" On a website:

 

<!DOCTYPEhtml>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

 

At Rootstack, we have a team of PHP experts prepared to design and create any project that your company needs to improve productivity and have a prominent presence in the technology market, a vital aspect to stay close to customers and users.

 

Contact us by clicking here and let's start working together.

 

We recommend you on video