What is array ? How to store data in array

 

What is array ? How to store data in array



                        



An array is a data structure in programming that allows you to store multiple values of the same data type under a single variable name. It is a collection of elements of the same type that are stored in contiguous memory locations.

In PHP, you can create an array using the following syntax:

php
$array_name = array(value1, value2, value3, ...);

or

bash
$array_name = [value1, value2, value3, ...];

Here is an example of an array in PHP:

php
$cars = array("Volvo", "BMW", "Toyota");

In the above example, we have created an array named $cars that contains three elements - "Volvo", "BMW", and "Toyota".

To add elements to an array, you can use the [] operator and specify the index at which you want to store the element. For example:

bash
$cars[3] = "Honda";

In the above example, we have added a new element "Honda" to the $cars array at index 3.

You can also use the array_push() function to add an element to the end of an array:

bash
array_push($cars, "Ford");

In the above example, we have added a new element "Ford" to the end of the $cars array.

To access the elements of an array, you can use the index number. For example:

php
echo $cars[0]; // Output: Volvo echo $cars[1]; // Output: BMW echo $cars[2]; // Output: Toyota

In the above example, we have accessed the first three elements of the $cars array using their index numbers.

Overall, arrays are a powerful tool in programming that allow you to store and manipulate collections of data efficiently.


Sagarsir sahu

Hello, my self Sagar sahu I am student in wainganga college engineering management University Nagpur

Post a Comment

Previous Post Next Post