Upcoming SlideShare
×

# Intoduction to php arrays

387 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
387
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
19
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Intoduction to php arrays

1. 1. Arrays
2. 2. Arrays Indexed Array int a[]={1,2,3,4,5,6,7,8,9,10}; for(i=0;i<10;i++) Printf(‚%d‛,a[i]); Indexed Array / Enumerated array \$a=array(1,2,3,4,5,6,7,8,9,10) for(\$i=0;\$i<10;\$i++) echo \$a[\$i] ; Associative Array \$a*‘name’+=‚John‛; \$a*‘age’+=24; \$a*‘mark’+=35.65; Foreach(\$a as \$key=>\$value) echo ‚ \$key contains \$ value‛; C PHP
3. 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. 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. 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. 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. 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. 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. 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
10. 10. Comparing Arrays == and === \$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
11. 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.
12. 12. Array Functions
13. 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
14. 14. Array functions – is_array() is_array() returns True if a variable contains array or else False \$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ; \$b=12 ; • echo count (\$a); // Outputs 6 • echo count(\$b); // outputs 1 • echo is_array(\$b) //outputs false
15. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
29. 29. Questions? ‚A good question deserve a good grade…‛
30. 30. Self Check !!