How to use an Arrays in PHP?
Hi friends,
I have recently started doing the PHP programming language, so I don't know much about the PHP. Before that I have used arrays but in different programming languages. I don't know how to use it in PHP. So, please tell me how to use an Arrays in PHP? Any coding related to the topic would be much grateful. !! :notworthy
Re: What are an Arrays in PHP?
Since you have used an arrays in other programming languages, I guess that you know why we exactly use it.! An array stores multiple values in one single variable. Using arrays in PHP can be a very powerful technique of handling data. Before knowing about an array, you should know about a variable. A variable is a storage area that holds a number or a text. But the variable holds only one value at a time. Whereas an array is known as a special variable that can store multiple values in one single variable.
Re: Example of an Arrays in PHP?
You can use the arrays for storing many values. Like, if you are having a list of items, for example lets say, colors, storing the colors in single variables, it will look like :
PHP Code:
$color1="Red";
$color2="Blue";
$color3="Green";
Each element in the array has its own index so that it can be easily accessed. An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Re: Different types of an Arrays in PHP?
There are three kind of arrays in PHP. They are described as follows :
- Numeric Array - A numeric array stores each array element with a numeric index. You can create a numeric arrays in two ways :
- You can let the index assign automatically.
- Or you can assign an index manually.
- Associative Arrays - Each ID key is associated with a value in an associative array. Its not necessary that numerical array is always best for storing data about specific named values. Many times associative array can be useful. You can use the values as keys and assign values to them by using an associative array.
- Multidimensional Arrays - In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array. Its very similar like a nested loop, if you consider a loop as an array.
Re: Example of an Arrays in PHP?
Once you have a look on the coding, I think that the things will be clear for you. So I have provided you with an example of an array :
PHP Code:
<?php
$ages['Sam'] = "22";
$ages['Searu'] = "20";
$ages['Sweady'] = "24";
echo "Sam is " . $ages['Sam'] . " years old.";
?>