intoduction to php arrays

32
Arrays

Upload: baabtracom-first-coding-school-in-india

Post on 24-May-2015

210 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Intoduction to php arrays

Arrays

Page 2: Intoduction to php arrays

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

Page 3: Intoduction to php arrays

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)

Page 4: Intoduction to php arrays

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

Page 5: Intoduction to php arrays

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.

Page 6: Intoduction to php arrays

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

Page 7: Intoduction to php arrays

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

Page 8: Intoduction to php arrays

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

Page 9: Intoduction to php arrays

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

Page 10: Intoduction to php arrays

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

Page 11: Intoduction to php arrays

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.

Page 12: Intoduction to php arrays

Array Functions

Page 13: Intoduction to php arrays

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

Page 14: Intoduction to php arrays

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

Page 15: Intoduction to php arrays

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

Page 16: Intoduction to php arrays

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

Page 17: Intoduction to php arrays

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

Page 18: Intoduction to php arrays

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

Page 19: Intoduction to php arrays

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

Page 20: Intoduction to php arrays

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

Page 21: Intoduction to php arrays

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

Page 22: Intoduction to php arrays

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.

Page 23: Intoduction to php arrays

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

Page 24: Intoduction to php arrays

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

Page 25: Intoduction to php arrays

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

Page 26: Intoduction to php arrays

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

Page 27: Intoduction to php arrays

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

Page 28: Intoduction to php arrays

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

Page 29: Intoduction to php arrays

Questions?

‚A good question deserve a good grade…‛

Page 30: Intoduction to php arrays

Self Check !!

Page 31: Intoduction to php arrays

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

Page 32: Intoduction to php arrays

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]