3. Printing Arrays
• PHP provides two functions that can be used to output a variable’s
value recursively
• print_r()
• var_dump().
Eg:
$a=array(12,13,’baabtra’’);
Var_dump($a);
Print_r($a);
//array(3) { [0]=> int(12) [1]=> int(13) [2]=> string(7) "baabtra‛}
// Array ( [0] => 12 [1] => 13 [2] => baabtra)
4. Arrays
• Outputs only the value and not the
datatype
• Cannot out put multiple varaiables at a
time
• Print_r returns upon printing
something
Var_dump Print_r
• outputs the data types of each value
• is capable of outputting the value of
more than one variable at the same time
•Doesn’t return anything
5. Multi Dimensional Arrays
To create multi-dimensional arrays, we simply assign an array as the
value for an array element
$a = array();
$a*+ = array(’foo’,’bar’);
$a*+ = array(’baz’,’bat’);
echo $a[0][1] . $a[1][0]; // outputa barbaz.
6. Array iterations
$array = array(’foo’, ’bar’, ’baz’);
foreach ($array as $key => $value) {
echo "$key: $value";
}
$a = array (1, 2, 3);
foreach ($a as $k => &$v) {
$v += 1;
}
var_dump ($a);
0 : foo
1 : bar
2 : baz
array(6) {
[0]=> int(2)
[1]=> int(3)
[2]=> int(4)
}
Out put
Out put
7. Unravelling Arrays
• It is sometimes simpler to work with the values of an array by
assigning them to individual variables. we do this by calling a
function named list()
• Eg:
$myarray = array (1, 2, 3);
list($a,$b,$c)=$myarray;
echo $b; //outputs 2
8. Array operations
• Array Union
$a = array (1, 2, 3);
$b = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
var_dump ($a + $b);
‚Here the result includes all of the elements of the two original arrays, even though
they have the same values; this is a result of the fact that the keys are different‛
array(6) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
["a"]=> int(1)
["b"]=> int(2)
["c"]=> int(3)
}
Out put
9. Array operations
$a = array (1, 2, 3);
$b = array (’a’ => 1, 2, 3);
var_dump ($a + $b);
‚if the two arrays have common elements that also share the same
keys, they would only appear once in the end result:‛
array(4) {
[0]=>int(1)
[1]=>int(2)
[2]=>int(3)
["a"]=>int(1)
}
Out put
11. Comparing Arrays
$a = array (1, 2, 3);
$b = array (1 => 2, 2 => 3, 0 => 1);
$c = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
var_dump ($a == $b); // True
var_dump ($a === $b); // False
var_dump ($a == $c); // True
var_dump ($a === $c); // False
As you can see, the equivalence operator == returns
true if both arrays have the same number of
elements with the same values and keys, regardless
of their order. The identity operator ===, on the
other hand, returns true only if the array contains
the same key/value pairs in the same order.
13. Array functions – count()
Count() returns the number of elements in an array
$a = array (1, 2, 4);
$b = array();
$c = 10;
echo count ($a); // Outputs 3
echo count ($b); // Outputs 0
echo count ($c); // Outputs 1 . ie count() cannot be used to determine whether
a variable contains an array or not
15. Array functions – isset()
• isset() used to determine whether an element with the given key
exists or not.
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
echo isset ($a*’a’+); // True
echo isset ($a*’c’+); // False
isset() has the major drawback of considering an element whose value is NULL
16. Array functions – array_key_exists()
array_key_exists() used to determine whether an element exists
within an array or not
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
$b=12 ;
echo array_key_exists ($a*’a’+); // True
echo array_key_exists ($a*’c’+); // True
17. Array functions – in_array()
in_array() used to determine whether a value exists within an array
or not
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
$b=12 ;
echo in_array ($a, 2); // True
18. Array functions- array_flip()
• array_flip() inverts the value of each element of an array with its
key
$a = array (’a’, ’b’, ’c’);
var_dump (array_flip ($a));
array(3) {
["a"]=>int(0)
["b"]=>int(1)
["c"]=>int(2)
}
Out put
19. Array functions- array_reverse()
• array_reverse() inverts the order of the array’s elements,so that
the last one appears first:
$a = array (’x’ => ’a’, 10 => ’b’, ’c’);
var_dump (array_reverse ($a));
array(3) {
[0]=>string(1) "c"
[1]=>string(1) "b"
["x"]=>string(1) "a"
}
Out put
20. Array functions – sort()
• sort() sorts an array based on its values
$array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
sort($array);
var_dump($array);
• ‚sort() effectively destroys all the keys in the array and renumbers its
elements‛
array(3) {
[0]=>string(3) "bar"
[1]=>string(3) "baz"
[2]=>string(3) "foo"
}
Out put
21. Array functions –asort()
• asort() :If you wish to maintain key association, you can use asort()
instead of sort()
$array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
asort($array);
var_dump($array);
array(3) {
["b"]=>string(3) "bar"
["c"]=>string(3) "baz"
["a"]=>string(3) "foo"
}
Out put
22. Parameters for sort() an asort()
• Both sort() and asort() accept a second, optional parameter that allows
you to specify how the sort operation takes place
• SORT_REGULAR : Compare items as they appear in the array, without
performing any kind of conversion. This is the default behaviour.
• SORT_NUMERIC : Convert each element to a numeric value for sorting
purposes.
• SORT_STRING : Compare all elements as strings.
23. Array functions –rsort() and arsort()
Both sort() and asort() sort values in ascending order. To sort them in
descending order, you can use rsort() and arsort().
$array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
arsort($array);
var_dump($array);
array(3) {
["a"]=>string(3) "foo"
["c"]=>string(3) "baz"
*"b"+=>string(3) "bar“
}
Out put
24. Array functions –natsort()
The sorting operation performed by sort() and asort() simply takes into consideration either
the numeric value of each element, or performs a byte-by-byte comparison of strings values.
This can result in an ‚unnatural‛ sorting order—for example, the string value ’10t’ will be
considered ‚lower‛ than ’2t’ because it starts with the character 1, which has a lower value
than 2. If this sorting algorithm doesn’t work well for your needs, you can try using
natsort() instead:
$array = array(’10t’, ’2t’, ’3t’);
natsort($array);
var_dump($array)
array(3) {
[1]=>
string(2) "2t"
[2]=>
string(2) "3t"
[0]=>
string(3) "10t"
}
Out put
25. Array functions –ksort() and krsort()
• PHP allows you to sort by key (rather than by value) using the
ksort() and krsort() functions, which work analogously to sort() and
rsort():
$a = array (’a’ => 30, ’b’ => 10, ’c’ => 22);
ksort($a);
var_dump ($a);
array(3) {
["a"]=>int(30)
["b"]=>int(10)
["c"]=>int(22)
}
Out put
26. Array functions –shuffle()
There are circumstances where, instead of ordering an array, you will want to
scramble its contents so that the keys are randomized; this can be done by
using the shuffle() function:
$cards = array (1, 2, 3, 4);
shuffle ($cards);
var_dump ($cards);
array(9) {
[0]=>int(4)
[1]=>int(1)
[2]=>int(2)
[3]=>int(3)
}
Out put
27. Array functions –array_rand()
If you need to extract individual elements from the array at random, you can
use array_rand(), which returns one or more random keys from an array:
$cards = array (’a’ => 10, ’b’ => 12, ’c’ => 13);
$keys = array_rand ($cards, 2);
var_dump ($keys);
var_dump ($cards);
array(2) {
[0]=>string(1) "a"
[1]=>string(1) "b"
}
array(3) {
["a"]=>int(10)
["b"]=>int(12)
["c"]=>int(13)
}
Out put
28. Array functions –array_diff()
array_diff() is used to compute the difference between two arrays:
$a = array (1, 2, 3);
$b = array (1, 3, 4);
var_dump (array_diff ($a, $b));
array(1) {
[1]=>
int(2)
}
Out put
31. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
32. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com