PHP ARRAYS
Hello All...
welcome every one. I'm going to start PHP lectures for Very Beginners, who
wants to learn PHP from very beginning. No matter if you do not have any basic
programming concept. If you follow my tutorials I hope you will surely get some
how good knowledge of PHP.
Today our
Topic is
ARRAY
Array is
special type of variable that stores similar or even multiple type of data
either number, characters, of alphanumeric characters within a single array. A
simple variable can store just a single value but an array can store multiple
values, as an ordered collection of key-value pairs. There are two types of an
array.
ð INDEXED AND
ASSOCIATIVE ARRAY
·
Indexed arrays are used when you identify things or values by their
position/indexes.
·
Key of indexed array are integer starts from 0. The
index of an array always starts from 0.
·
Associative array string as keys and behaves like two column table.
·
Sometimes number is not appropriate for identifying
any array value for that we assign names to them, that’s what we need an
associative array.
ð Declaring an
Array
i-
Indexed Array
There are two ways by which an array can be declared.
$names =
array(“John”, “Celina”, “Rock”, “Catherine”);
It’s
important to write array keyword as it shows that the declared variable is array
type variable. Each of above value is automatically assigned a key to that
values “John”, “Celina”, “Rock”, “Catherine” starts from 0,1,2,3 respectively
Another way
is assigning the key manually.
$names[0] =
“John”;
$names[1] =
“Celina”;
$names[2] =
“Rock”;
$names[3] =
“Catherine”;
Both of the
ways are workable and second example is more clear I think, that at the 0 index
of an array there is value that is “John” and at the 1st index of an
array there is value that is “Celina” and so on respectively. Let’s do the
example
<?php
$names[0] = “John”;
$names[1] = “Celina”;
$names[2] = “Rock”;
$names[3] = “Catherine”;
echo $names[0] . “ and “ . $names[2]
. “ are brothers to each other”;
?>
And the
result will be like this:
John and
Rock are brothers to each other.
ii-
Associative Array
Just like
Indexed an Associative array can be declared in two ways same as an indexed
array, the difference is in indexed array key of any value is integer but in
associative array key of value will be string. To create an associative array
use `=>` symbol to separate indexes from values. Example
$student =
array(“First_Name”=> ”John”, “Last_Name”=> “Smith”, “Age”=>”22”);
Here key is
a string not a number and value is integer as well string. Look for another
example of declaring an associative array.
$student[“First_Name”]
= “John”;
$student[“Last_Name”]
= “Smith”;
$student[“Age”]
= “22”;
Just very
simple and easy. Now print out some result.
<?php
echo "Age of " .
$student["First_Name"] . " is " .
$student["Age"];
?>
This will
result as: Age of John is 22.
If you want
to see the structure and values of an array you can use two of the common
functions print_r(array_variable) and var_dump(array_name), however print_r()
gives less information as compared to var_dump(). So it is preferred to use
var_dump() you can use and see the information about an array.
var_dump($student)
or var_dump($names) or any of the array you have declared.
Hope that
from above example it’s clear that an array can store multiple type of values
and also you can print anything according to your need when needed. But
remember you cannot initialize same key of indexed or associative array. Key
should always be unique.
Thanks Guys.
Any thing missing or any question from this topic you can comment below, I will
be trying to solve and answer your question.
Our Next
topic will be `Database`.
Good Luck J
No comments:
Post a Comment