php arrays
Download
Skip this Video
Download Presentation
PHP Arrays

Loading in 2 Seconds...

play fullscreen
1 / 21

PHP Arrays - PowerPoint PPT Presentation


  • 530 Views
  • Uploaded on

PHP Arrays An indexed array is similar to one provided by a conventional programming language. An element of an associative array can be accessed by a keyword. An associative array is like a dictionary or map . An array element can be of any type.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'PHP Arrays' - omer


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
php arrays
PHP Arrays
  • An indexed array is similar to one provided by a conventional programming language.
  • An element of an associative array can be accessed by a keyword.
  • An associative array is like a dictionary or map.
  • An array element can be of any type.
  • An array can be heterogeneous with its element types and structure.
  • Many functions manipulating an array are provided.
indexed array
Indexed Array

$animals =

array("dog", "cat", "fish");

echo "$animals[0]\n";

echo "$animals[2]\n";

echo "$animals\n";

dog

fish

Array

updating and adding elements
Updating and Adding Elements

$animals =

array("dog", "cat", "fish");

echo "$animals[1]\n";

$animals[1] = "tiger";

echo "$animals[1]\n";

$animals[] = "beaver";

echo "$animals[3]\n";

cat

tiger

beaver

associative array
Associative Array

$animals = array(

"dog“ => 15,"cat“ = >8, "fish“ => 2);

echo "$animals[cat]\n";

$animals["bat"] = 100;

echo "$animals[bat]\n";

8

100

listing array element for
Listing array element: for

$animals =

array("dog", "cat", "fish");

for ($i = 0; $i < count($animals); $i++) {

echo $i . "-th animal is a $animals[$i].\n";

}

0-th animal is a dog.

1-th animal is a cat.

2-th animal is a fish.

listing array elements foreach
Listing Array Elements: foreach

$animals =

array("dog", "cat", "fish");

foreach ($animals as $animal)

echo "$animal\n";

}

dog

cat

fish

while and each
while and each

$animals = array(

"dog“ => 15,"cat“ => 8, "fish“ => 2);

while ($item = each($animals))

print "weight of " .

$item["key"] . " is " .

$item["value"] . “.\n";

weight of dog is 15.

weight of cat is 8.

weight of fish is 2.

each and list
each and list

$animals = array(

"dog“ => 15, "cat“ => 8, "fish“ => 2);

while (list($key, $value) = each($animals))

print "weight of $key is $value.\n";

weight of dog is 15.

weight of cat is 8.

weight of fish is 2.

multi dimensional heterogeneous array
Multi-Dimensional Heterogeneous Array

$books = array(

array("title“ => “A",

"author“ => “X"),

array("title“ => “B",

“author“ => “Y",

“price“ => 25)

);

print_r($books);

Array

(

[0] => Array

(

[title] => A

[author] => X

)

[1] => Array

(

[title] => B

[author] => Y

[price] => 25

)

)

nested loops
Nested Loops

for ($i=0; $i < count($books); $i++) {

print "$i-th book is:";

while ( list($key, $value) =

each($books[$i]) )

print “ $key: $value";

print "\n";

}

0-th book is: title: A author: X

1-th book is: title: B author: Y price: 25

string as an array
String as an Array

$myString = "My chars";

echo "$myString\n";

echo "$myString[1]\n";

My chars

y

array functions
Array functions
  • count(), sizeof()
  • in_array()
  • array_slice()
  • array_pad()
  • array_reverse()
  • list( )
  • Sorting Functions
    • sort(), rsort()
    • asort(), arsort()
    • ksort(), krsort()
count array1 and sizeof array1
count(array1)andsizeof(array1)

Returns the size of array array1.

$animals = array ('dog', 'cat', 'fish');

echo count($animals);

echo sizeof($animals);

3

3

array array pad array1 length value
array array_pad(array1, length, value)

Pad array to length length with value.

Array

(

[0] => 1

[1] => 2

[2] => 10

[3] => 10

[4] => 10

)

$scores =

array(1, 2);

$padded =

array_pad($scores, 5, 10);

print_r($padded);

array array reverse array1
array array_reverse(array1)

Return an array with elements in reverse order.

$animals =

array('dog', 'cat', 'fish');

$reversed array_reverse($animals);

print_r($reversed);

Array

(

[0] => fish

[1] => cat

[2] => dog

)

array array slice array1 offset length
array array_slice (array1, offset, length)

Extract a slice of length from array1 starting at

offset.

$array1 =

array(1, 2, 3, 4, 5, 6);

$subarray =

array_slice(array1, 3, 2);

print_r($subarray);

Array

(

[0] => 4

[1] => 5

)

boolean in array value array1
boolean in_array(value, array1)

Check if value exists in array1.

$animals = array('dog', 'cat', 'fish');

echo in_array('cat', $animals);

echo in_array(‘monkey', $animals);

1 (true)

(false)

void list var1 var2
void list(var1, var2, ...)

The elements of an array are copied into the list

of variables var1, var2, . . .

$animals = array('dog', 'cat', 'fish');

list($a, $b, $c) = $animals;

echo "$a, $b, $c";

dog, cat, fish

sort array and rsort array
sort(array)and rsort(array)

Sort the elements in an array in increasing or

decreasing order.

$animals =

array('dog', 'cat', fish');

sort($animals);

print_r($animals);

Array

(

[0] => cat

[1] => dog

[2] => fish

)

asort array arsort array
asort(array), arsort(array)

Sort an array maintaining index association.

Array

(

[1] => cat

[0] => dog

[2] => fish

)

$animals =

array('dog', 'cat', 'fish');

asort($animals);

print_r($animals);

ksort array krsort array
ksort(array), krsort(array)

Sort array by keys.

$animals =

array('dog' => 15,

'cat' => 8,

'fish' => 2);

ksort($animals);

print_r($animals);

Array

(

[cat] => 8

[dog] => 15

[fish] => 12

)

ad