php array

27
By Nikul Shah Nikul Shah 1

Upload: nikul-shah

Post on 16-Jul-2015

93 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Php array

By Nikul Shah

Nikul Shah 1

Page 2: Php array

Nikul Shah 2

Page 3: Php array

An array is assigned to a single variable, but it can hold dozens of individual pieces of information.

Each individual bit of information, or row, is referred to as an array element.

Within every array element there are two parts: the value, which contains the actual information you want to store, and a unique key which identifies the value.

you will learn later how key palys an importanat role in the process of accessing and manipulating array elements.

Nikul Shah 3

Page 4: Php array

Keys can either be non-negative integers or strings.

Array with integers as key are the most common type and known as SCALAR ARRAYs.

Those with key as string are known as ASSOCIATIVE ARRAY.

Nikul Shah 4

Page 5: Php array

There are 2 kinds of arrays in php: indexed and associative arrray.

Key of indexed array is integer starts with 0.

Indexed array are used when we identify things with their position.

Associative arrays have strings as keys and behave more like two-columns table.

The first column is key and second is use to access value.

Nikul Shah 5

Page 6: Php array

Numeric array

Associative Array

Multidimensional Array.

Nikul Shah 6

Page 7: Php array

In numeric array have to create only elements, id will be automatically assigned to the elements.

example <?php $a = array("train", "car","bus"); print_r($a); ?> output Array ( [0] => train [1] => car [2] => bus )

Nikul Shah 7

Page 8: Php array

In associative array we have create element with id.

example <?php $a =

array("6"=>"train","7"=>"car","8"=>"bus"); print_r($a); ?> Out put. Array ( [6] => train [7] => car [8] => bus )

Nikul Shah 8

Page 9: Php array

Multidimensioanl array means we have to create array within array.

example

<?php

$a = array("a","b"=>array("train","car"),"bus");

print_r($a);

?>

Out put.

Array ( [0] => a [b] => Array ( [0] => train [1] => car ) [1] => bus )

Nikul Shah 9

Page 10: Php array

Create an array by using the elements from one "keys" array and one "values" array:

<?php $fname=array("Peter","Ben","Joe"); $age=array("35","37","43"); $c=array_combine($fname,$age); print_r($c); ?>

Nikul Shah 10

Page 11: Php array

This function is for counting the values of an array.

<?php

$no=array(0=>"5",1=>"10",2=>"15",3=>"15");

print_r(array_count_values($no));

?>

Nikul Shah 11

Page 12: Php array

Compare the values of two arrays, and return the differences(difference value is displayes of first array.)

<?php

$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","b"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

print_r(array_diff($animal1,$animal3));

?>

Nikul Shah 12

Page 13: Php array

Merge two arrays into one array:

<?php

$animal1=array("a"=>"lion","b"=>"tiger");

$animal2=array("c"=>"dog","d"=>"tiger");

$animal3=array("a"=>"cow","g"=>"tiger");

print_r(array_merge($animal1,$animal2,$animal3));

?>

Nikul Shah 13

Page 14: Php array

Merge two arrays into one array:

<?php

$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

print_r(array_merge_recursive($animal1,$animal2

,$animal3));

?>

Nikul Shah 14

Page 15: Php array

Returns an array in the reverse order <?php

$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

print_r(array_reverse($animal1));

?>

Nikul Shah 15

Page 16: Php array

Searches an array for a given value and returns the key

<?php

$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

print_r(array_search("lion"$animal1));

?>

Nikul Shah 16

Page 17: Php array

The array_sum() function returns the sum of all the values in the array.

<?php $a=array(5,15,25); echo array_sum($a); ?>

Nikul Shah 17

Page 18: Php array

This function sorts an associative array in desending order, according to the value.

<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d

"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

arsort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>

Nikul Shah 18

Page 19: Php array

This function sorts an associative array in ascending order, according to the value

<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=

>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

asort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>

Nikul Shah 19

Page 20: Php array

This function sorts an associative array in desending order, according to the key.

<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=>"tiger

"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

arsort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>

Nikul Shah 20

Page 21: Php array

This function sorts an associative array in ascending order, according to the key

<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=

>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

asort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>

Nikul Shah 21

Page 22: Php array

This function is to find out no. of elements in an array.

<?php

$animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=>"tiger");

$animal2=array("c"=>"dog","d"=>"tiger");

$animal3=array("a"=>"cow","g"=>"tiger");

asort($animal1);

print_r(sizeof($animal1));

?>

Nikul Shah 22

Page 23: Php array

Removes the first element from an array and returns the value of the removed element.

<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=

>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");

echo array_shift($animal1); echo "<br>"; print_r($animal1); ?>

Nikul Shah 23

Page 24: Php array

Return an array of random keys:

<?php

$input=array("a","b","c","d","e","f","g","h",);

$rand_keys = array_rand($input,2);

echo $input[$rand_keys[0]]."\n";

echo $input[$rand_keys[1]]."\n";

?>

Nikul Shah 24

Page 25: Php array

array_push() function inserts one or more elements to the end of an array.

You can add one value, or as many as you like.

Even if your array has string keys, your added elements will always have numeric keys.

Nikul Shah 25

Page 26: Php array

<?php

$a=array("a"=>"tree",'b'=>"car");

array_push($a,"bike","activa");

print_r($a);

?>

Nikul Shah 26

Page 27: Php array

array_pop() function deletes the last element of an array.

<?php

$a=array("tree","car","bike");

array_pop($a);

print_r($a);

?>

Nikul Shah 27