PHP ARRAY

Like another programming language php also having array. Array is a special variable which can used to store multiple values of same data type.

Array is used to hold all values under one single name and we used that value by referring that name of array. Array can be accessed by its index value.

Type of array in PHP

  1. 1.Numeric Array.
  2. 2.Associative Array.
  3. 3.Multidimensional Array

 

  • Numeric Array: - In numeric array store each element with a numeric index.

There are generally two methods to create a numeric array:

  1. a)$computer = array("Acer”,”Lenovo”,”deal”,”hp”);

In this the index value are automatically assigned to each array element starts from 0.

  1. b)$computer[0] = “Acer”;
  2. c)$computer[1] = “Lenovo”;
  3. d)$computer[2] = “deal”;
  4. e)$computer[3] = “hp”;

                    In this we assign the index value to each element in array.

            Example:

                       

                        $computer[0] = “Acer”;

                        $computer[1] = “Lenovo”;

                        $computer[2] = “ deal”;

                        $computer[3] = “hp”;

                        Echo $computer[0].”and”.$computer[1].”are cheap and best”;

                        ?>

            Output: Acer and Lenovo are cheap and best

  • Associative Array: In this each id is associative with a value.

 

Numeric array is not best in storing data about specific named so we can use associative array. With associative array we can use the value as key and assign to them.

Example :

                  $employee = array (“Ram”=>23,”Sham”=>24,”Pankaj”=>23);

                                                      Or

                  $employee [‘Ram’] =”23”;

                  $employee [‘Sham’] = “24”;

                  $employee [‘Pankaj’] = “23”;

Example:

                 

                  $employee [‘Ram’] =”23”;

                  $employee [‘Sham’] = “24”;

                  $employee [‘Pankaj’] = “23”;

                  echo “Ram is”.$employee[‘Ram’].”Year old”;

                  ?>

Output: Ram is 23 year old.

  • Multidimensional Array: in this an array element contain array or we can say that each element in the main array can also be an array. And each element in the sub-array can be an array.

 

Example:

                        Array

                        (

                        [kumar]=> array

                                    (

                                                [0] =>pankaj

                                                [1]=>amit

                                    )

                        [singh]=> array

                                    (

                                                [0]=> sumit

                                    )

                        ) 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Do you think love marriage is better than arranged marriage?

Do you think love marriage is better than arranged marriage?
No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 2 answer(s).
/polls/miscellaneous/6090-do-you-think-love-marriage-is-better-than-arranged-marriage.json?task=poll.vote
6090
radio
[{"id":"20995","title":"Yes","votes":"91","type":"x","order":"0","pct":71.09,"resources":[]},{"id":"20996","title":"NO","votes":"37","type":"x","order":"0","pct":28.91,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
To secure our future and to have our life safe and jolly we tend to have more wealth in future than now. For that investment has always been the inevitable
Fascism is a philosophy that is as old as history. It really means that when there is chaos in society then the government must take over with an Iron
Vladimir Lenin has an important place in the 20th century. Unfortunately, his name is associated with more negative qualities than positive qualities.
Dictatorship is a one man rule or centralized rule.After the end of  World War I in 1918, the representative democratic system was adopted all over
The great Mauryan emperor Ashoka ascended the throne of Magadh in the year 273 B.C.He as the son of the Mauryan emperor Bindusara and the grandson of Chandragupta