PHP Array

PHP Array

In this article, we’ll learn about PHP Array.

Arrays are one of the most fundamental and powerful data structures in PHP. They play a crucial role in organizing, storing, and manipulating data efficiently. Whether you’re a beginner or an experienced PHP developer, understanding arrays and their various features is essential.

We’ll dive deep into the world of PHP arrays, exploring their syntax, functions, and best practices to help you harness their full potential.

What is PHP Array

Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. An array is an special variable which can hold more than one value at a time.

So why we use array? we use array for to stores multiple values…like this…

  1. $laptop_brands = array(“Dell”,“Samsung”,“Asus”,“Lenevo”);

So How to create array? we use array function to create array.

Types Of Array

There are three different types of arrays in PHP:

a) Numeric Array: An array with a numeric ID key.
b) Associative Array: An array where each ID key is associated with a value.
c) Multidimensional Array: An array containing one or more arrays.

a). Numeric Arrays:

Numeric arrays use integer / numbers as their index number to identify each item of the array. The example we discussed above are numeric arrays as they have integer values as index numbers for each item.

<?php
$colours = array("white","black","blue");

print_r($colours);

/*
output will be

Array
(
[0] => white
[1] => black
[2] => blue
)
*/
?>

In the above output, you can see the index numbers for white, black, and blue are 0,1,2 respectively which are numeric values and hence we call such arrays numeric arrays.

b). Associative Arrays:

Sometimes it’s better to use the index name instead of index number for example if you want to save three students’ names and numbers so your best option will be to use each student’s name as index value for the array and his numbers as the values, behold on the example below,

<?php

$students['Jack'] = 90;
$students['Jane'] = 60;
$students['Hannah'] = 40;

echo "Hannah's mark in maths is" . $students['Hannah'] ;

/* Output is: Hannah's mark in maths is 40 */
?>

When you submit a form using the POST or GET method you get a similar associative array on the receiving page that contains the name of each form field as array index and its value as the index value. Try to make an HTML form with some fields and post it and on the receiving page print the global arrays like

print_r($_POST);
print_r($_GET);

and you will see the associative array.

Associative Arrays are easier to handle and to process information especially dealing with complex form submission and dynamic values from databases etc.

c). Multidimensional Arrays:

A multidimensional array can contain arrays within itself and the sub-arrays contain more arrays within them.

This is how you can use multidimensional arrays to organize data. Try to submit an array of form fields and then print the global array to check the output, you will get the global array as a multidimensional array that will contain more sub-arrays.

Please check following example:

<?php
// Define nested array
$contacts = array(
array(
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
),
array(
"name" => "Jack Reacher",
"email" => "jreacher@mail.com",
),
array(
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
)
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];

/*

Output is : Peter Parker's Email-id is: peterparker@mail.com

*/
?>

PHP arrays are a cornerstone of web development, offering immense flexibility and versatility. By delving into the intricacies of arrays, you can unlock their full potential and streamline your PHP code. This comprehensive guide has provided a solid foundation, covering array basics, manipulation techniques, multidimensional arrays, best practices, performance considerations, and advanced php array functions.

Armed with this knowledge, you’re now ready to tackle any array-related challenge and elevate your PHP coding skills to new heights. Happy coding with PHP arrays!

I Hope this article helps you to understand the overview of PHP array

One thought on “PHP Array

Write a Reply or Comment

Your email address will not be published. Required fields are marked *