Monday, March 3, 2014

Basic part-5 | Arrays

Hello ... friends today we are going to discus about the array. suppose if you want to store the data of various employee in your company then we can't store the data in different different variables & such that we increase just number of codes in our scripts so here comes the role of arrays in our scripts so here we just simply define a single array variable $employees which will hold our all data

So here in this post we will study about array :
  • How array works in php
  • How to create array in php
  • How to access elements of array
  • How to use loops in array 

if we are going to add an additional feature in array then let's add why we use array :p
ok we have mainly two features in php

  • Array can be of any length
  • Easy to manipulate all values in array at once
How array works in php : as other programming array also mainly support two types of arrays
  • Indexed array : where each element is refernenced by numaric index. starting from zero for example 0,1,2,3 ... so on.
  • Associative array : as refered as a hash or map here each element is referenced by a string index example as we create an array elements of an employee's age and give it as index of "age".
  • Multidimensional Array : array will become much more powerful with multidimensional array , we can store all the types of value in the array include objects, string, integers and also any other array too , when we store an array inside an array then it known as multidimensional array.
An array index is often referred to as a key. Typically, a numeric index is called an index and a string index is called a key same as book index 

How to create an array : we can create an array variable by using build in array () construct example as 

$employees = array( "Amit" , "Jitender" , "Yash" , "Pardeep" , "Gandhi" );

we can access this array or any member just by using an array variable $employees this is a simple index array & we can accessed it just by using numeric index starting with zero . as index of 1 is Jitender , Index of 2 is Yash , Index of 4 is Gandhi . 
Now let me create an associative array : 

$myBook = array ( "title" => "The Hackers Code" ,
                               "author" => "Govind Singh"
                               "PubYear" => "2016" );

here it create an array with three elements: "The Hackers Code" , which has an index of "title"; "Govind Singh", which has an index of "author" ; and 2016, which has an index of "PubYear".

How to access elements of array : 
ok now we create an array with various values then how to access any particular value ? It's similar ways as accessing an Individual characters within a string . Here we can call any particular value with Index as shown in image or we can say you write the variable name , followed by index of the elements. If we want to access the associative array we need to use simple string inside  rather than number .



How to change the record in array :
we can create an array call any particular value from array & now what if any value from record changed then How we can change it from entire array ?  How we can update record in array ? & How to add a new value in any array let we understand it all by Live example


In this example we can see clearly that in indexed type array we can update & insert a new record simply just by using indexed value & in associated array just calling key value .... it's vary simple

Testing array with useful functions : 

  • Print all array --- print_r()
  • Extracting a Range of element --- array_slice()
  • Counting Elements --- count()
  • Navigation through Array -- current() , key () , next () , prev () , end () , reset ()




Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

© 2017 Friends Code
Designed by cyb3r.gladiat0r
Posts RSSComments RSS
Back to top
Hack for India, Code for India, Configuration for India, Die for India !