arrays(2)
arrays(2)
Arrays, functions
and graphics
arrays
An array stores multiple values in one
single variable:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . "
and " . $cars[2] . ".";
?>
What is an Array?
An array is a special variable, which can hold more
than one value at a time.
If you have a list of items (a list of car names, for
example), storing the cars in single variables could
look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars
and find a specific one? And what if you had not 3
cars, but 300?
The solution is to create an array!
An array can hold many values under a single name,
and you can access the values by referring to an index
number.
Create an Array in PHP
index
Associative arrays - Arrays with named
keys
Multidimensional arrays - Arrays
<?php
Output
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
3PHP Multidimensional Arrays
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
</body>
</html>
Output
Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
Array extract ()function
The extract() function imports variables
into the local symbol table from an array.
This function uses array keys as variable
names and values as variable values. For
each element it will create a variable in
the current symbol table.(array to
variable conversion)
This function returns the number of
variables extracted on success.
Syntax
extract(array, extract_rules, prefix)
PHP extract() Function
in array
<html>
<body>
<?php
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" =>
"Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
</body>
</html>
Output:
$a = Cat; $b = Dog; $c = Horse
PHP array_flip() Function
Flips/Exchanges all keys with their associated values
in an array
<html>
<body>
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"
=>"yellow");
$result=array_flip($a1);
print_r($result);
?>
</body>
</html>
Output
Array ( [red] => a [green] => b [blue] => c [yellow]
=> d )
Traversing array
Traversing an array means to visit each
and every element of array using a
looping structure and iterator function
Several ways to traverse array in php
1)Using foreach construct
2)Using for loop
3)Using Iterator function
1)The foreach Construct:- The most common way
to loop over elements of an array is to use the
foreach construct: Syntax
foreach($array as $value){ statement }
$a=array(‘aaa’,’bbb’,’ccc’);
foreach($a as $value)
{
Echo “$value\n”;
}
Output:
aaa
bbb
ccc
1)Associative array example
Syntax
foreach($array as $value){ statement }
$age=array(‘raj’=>1, ’simran’=>2);
foreach($age as $key=> $value)
{
Echo “$key rollno is $value \n”;
}
Output:
raj rollno is 1
simran rollno is 2
2) Using a for Loop
if you know that you are dealing with an indexed
Echo key($a); 0
Print_r(each($a)); output
Array (
[key]>>0
[value]>>sanjay
PHP implode() Function
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);
?>
Output
Hello World! Beautiful Day!
</body> Hello+World!+Beautiful+Day!
</html> Hello-World!-Beautiful-Day!
HelloXWorld!XBeautifulXDay!
PHP explode() Function
the explode() function breaks a string
into an array.
Syntax
explode(separator,string,limit)
Parameter Description
separator Required. Specifies where to break the string
string Required. The string to split
limit Optional. Specifies the number of array elements to
return.Possible values:
•Greater than 0 - Returns an array with a maximum
of limit element(s)
•Less than 0 - Returns an array except for the last -
limit elements()
•0 - Returns an array with one element
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Hello world. It's a beautiful day.“;
print_r (explode(" ",$str));
?>
</body>
</html>
Output
Array ( [0] => Hello [1] => world. [2] => It's
[3] => a [4] => beautiful [5] => day.