17
Array & Foreach Array & Foreach อออออออออออออออออออออออ อออออออออออออออออออออออ

Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Embed Size (px)

Citation preview

Page 1: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Array & ForeachArray & Foreachอาร์�เร์ย์�และคำ�าสั่ �งวนล�ปอาร์�เร์ย์�และคำ�าสั่ �งวนล�ป

Page 2: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

ContentContent

1. Definition and Usage

2. Syntax

3. print_r() Statement

4. For and Foreach

5. Array Functions

Page 3: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Definition and Usage

The array() function is used to create an array.In PHP, there are three types of arrays:

Indexed arrays - Arrays with numeric indexAssociative arrays - Arrays with named keysMultidimensional arrays - Arrays containing one or more arrays

Page 4: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Syntax

Syntax for indexed arrays:

Syntax for associative arrays: 

array(value1,value2,value3,etc.);

array(key=>value,key=>value,key=>value,etc.);

Page 5: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Syntax

Page 6: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

<?php$cars=array("Volvo","BMW","Toyota");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

// การใช้�เคร�องหมาย . เป็�นการเช้�อมป็ระโยคเข้�าด้�วยก�น?>

Syntax

Page 7: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

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

echo "Peter is " . $age['Peter'] . " years old.";?>

Syntax

Page 8: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

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

echo "Peter is " . $age['Peter'] . " years old.";?>

Syntax

Page 9: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

PHP 4, PHP 5

print_r — Prints human-readable information about a variable

print_r Statement

Source: http://php.net/manual/en/function.print-r.php

Page 10: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

<?php$b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));$results = print_r($b); // $results now contains output from print_r?>

print_r Statement

Array ( [m] => monkey [foo] => bar [x] => Array ( [0] => x [1] => y [2] => z ) )

Page 11: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

<?php$cars=array("Volvo","BMW","Toyota");$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)  {  echo $cars[$x];  echo "<br>";  }?>

For()

Loop through and print all the values of an indexed array:

Page 12: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

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

foreach($age as $x=>$x_value)  {  echo "Key=" . $x . ", Value=" . $x_value;  echo "<br>";  }?>

Foreach()

Loop through and print all the values of an associative array:

Page 13: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Array Functions

Page 14: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Array Functions count($ar) - How many elements in an

array is_array($ar) - Returns TRUE if a variable is

an array sort($ar) - Sorts the array values (loses

key) ksort($ar) - Sorts the array by key asort($ar) - Sorts array by value, keeping

key association shuffle($ar) - Shuffles the array into

random order

Page 15: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Array and String

$txt = “This is a book !”;$ar = explode(' ', $txt);print_r($ar);

Array ( [0] => This [1] => is [2] => a [3] => book! )

Page 16: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Summary PHP arrays are a very powerful associative

array as they can be indexed by integers like a list, or use keys to look values up like a hash map or dictionary

There are many options for sorting We can use explode() to split a string into

an array of strings

Page 17: Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Q & A