PHP array() Function

In this article, you will explore the capabilities of the Func Array / array() function in PHP. You will start with a brief introduction to the concept of arrays, including how they are created and populated.

From there, you will delve into more advanced topics. By the end of this article, you will have a clear understanding of how arrays work in PHP, allowing you to apply these skills to a variety of technical programming tasks that you may encounter in the future. With the Func Array / array() function, you have the power to store and manipulate data in an array.



What is array() function in PHP?

The array() function in PHP is a built-in function that is used to create arrays. It is a versatile function that allows you to create both indexed arrays (arrays with numeric keys) and associative arrays (arrays with string keys).

There are three types of array in PHP:

  • Indexed arrays – With numerical indices.
  • Associative arrays – Key-value arrays.
  • Multidimensional arrays – arrays containing multiple arrays.

Syntax

array(value1, value2, value3, etc.)

Syntax for associative arrays

array(key=>value,key=>value,key=>value,etc.)

You can create arrays of any size, with any type of data, and store information within them. Whether you need to store a collection of strings, an array of integers, or a combination of both, this function has you covered.

Example: 

<?php $flowers = array("Sunflower", "Tulip", "Rose"); echo "I like " . $flowers[0] . ", " . $flowers[1] . " and " . $flowers[2] . "."; ?>

When we talk about Func Array then, Associative array named $user_info should be created as follows:

Example: 

<?php $user_info = array( "FirstName"=>"Mark", "LastName"=>"Zuckerberg", "Age"=>38, "Gender"=>"M", ); echo $user_info["FirstName"] . " " . $user_info["LastName"]. " is " . $user_info["Age"] . " years old. <br>"; echo "And he is the CEO of Facebook"; ?>

Following is an example of looping through each element of an associative array:

Example: 

<?php $user_info = array( "FirstName"=>"Mark", "LastName"=>"Zuckerberg", "Age"=>38, "Gender"=>"M", ); foreach($user_info as $field => $value) { echo $field . " = " . $value. "<br>"; echo " "; } ?>
Creating an array with multiple dimensions is as simple as these steps:

Example: 

<?php $fruits = array( array("Apple",35,22), array("Banana",15,10), array("Mango",48,35), array("Grapes",22,15) ); ?>

Conclusion

The array() function in PHP OR Func Array is a powerful tool that allows you to store and manipulate data in an array. Whether you need to create an array, access its elements, loop through its elements, add elements to it, or sort its elements, the array() function has you covered. By mastering the array() function, you will be able to perform a wide range of tasks in PHP with ease.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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