1 4 ARRAY array types foreach loop Use

  • Slides: 24
Download presentation
1 4 ARRAY ● array types ● foreach loop ● Use arrays with Web

1 4 ARRAY ● array types ● foreach loop ● Use arrays with Web forms ● Built-in functions PHP อ. ธระพล ลมศรทธา

การกำหนด อารเรย 4 <? php // Type 1. define array $frults = array(‘apple’, ‘grap’,

การกำหนด อารเรย 4 <? php // Type 1. define array $frults = array(‘apple’, ‘grap’, ‘pinapple’, ‘banan’); //Type 2. $cars[0 ] = 'Ferrari'; $cars[1 ] = 'Lamborghini'; ? >

6 การกำหนดคาทำไดสองแบบสำหรบการ ใชคย <? php // define array $data['username'] = 'john'; $data['password'] = 'secret';

6 การกำหนดคาทำไดสองแบบสำหรบการ ใชคย <? php // define array $data['username'] = 'john'; $data['password'] = 'secret'; $data['host'] = '192. 168. 0. 1'; ? > <? php // define array $data = array( 'username' => 'john', 'password' => 'secret', 'host' => '192. 168. 0. 1' ); ? >

การอานชอคย 7 <? php // define array $data = array( 'username' => 'john', 'password'

การอานชอคย 7 <? php // define array $data = array( 'username' => 'john', 'password' => 'secret', 'host' => '192. 168. 0. 1' ); echo array_keys($data)[0]; //print username ? >

8 การเขาถง อารเรยดวยคยอนเดกซ <? php // define array $data = array( 'username' => 'john',

8 การเขาถง อารเรยดวยคยอนเดกซ <? php // define array $data = array( 'username' => 'john', 'password' => 'secret', 'host' => '192. 168. 0. 1' ); // use array value Echo ? >

การปรบปรงคาในอารเรย 9 <? php // define array $meats = array( 'fish', 'chicken', 'ham', 'lamb‘

การปรบปรงคาในอารเรย 9 <? php // define array $meats = array( 'fish', 'chicken', 'ham', 'lamb‘ ); // change 'ham' to 'turkey' $meats[2] = 'turkey'; ? >

หาขนาดของอะเรย 11 count() หรอ sizeof( ) function ตวอยางการใช <? php // define array $data

หาขนาดของอะเรย 11 count() หรอ sizeof( ) function ตวอยางการใช <? php // define array $data = array('Monday', 'Tuesday', 'Wednesday'); // get array size echo 'The array has '. count($data). ' elements'; ? >

Nested Array 12 <? php $phonebook = array( 'name' => 'Raymond‘, 'tel' => '1234567‘,

Nested Array 12 <? php $phonebook = array( 'name' => 'Raymond‘, 'tel' => '1234567‘, 'email' => 'ray@pnet. in‘, ), array( 'name' => 'Harold‘, 'tel' => '5942033‘, 'email' => 'harold@tuff. com‘, ) ); echo $phonebook[1]['tel']; //print 5942033 ? >

Loop in Array 13 <? php $cities = array('London', 'Paris', 'Madrid', 'Bombay', 'Jakarta'); //

Loop in Array 13 <? php $cities = array('London', 'Paris', 'Madrid', 'Bombay', 'Jakarta'); // iterate over array for ($i=0; $i<count($cities); $i++) { echo $cities[$i]. "<br/>"; } ? >

การอาน nested array 14 1. <? php 2. $phonebook = array( 3. array('Raymond', '1234567',

การอาน nested array 14 1. <? php 2. $phonebook = array( 3. array('Raymond', '1234567', 'ray@pnet. in', ), 4. array('Harold', '5942033', 'harold@tuff. com', ) 5. ); for ($i=0; $i<count($phonebook); $i++){ 6. echo $i ; 7. for($j = 0; $j<count($phonebook[$i]); $j++){ 8. echo $phonebook[$i][$j]; 9. } 10. echo ' '; 11. 12. } 13. ? >

foreach loop 15 foreach ($cities as $value) { echo "$value <br/>"; } foreach ($cities

foreach loop 15 foreach ($cities as $value) { echo "$value <br/>"; } foreach ($cities as $key=>$value) { echo “Key: ”. $key. “ Value: ”. $value. ”<br/>"; }

Try: Create data Table 16 <? php $phonebook = array( 'name'=>'Raymond', 'tel'=>'1234567', 'email'=>'ray@pnet. in',

Try: Create data Table 16 <? php $phonebook = array( 'name'=>'Raymond', 'tel'=>'1234567', 'email'=>'ray@pnet. in', ), array( 'name'=>'Harold', 'tel'=>'5942033', 'email'=>'harold@tuff. com', ) ); ? > ใหใชคำสง foreach

Array. Iterator (New PHP 5. 0( 17 $cities = array( "United Kingdom" => "London“,

Array. Iterator (New PHP 5. 0( 17 $cities = array( "United Kingdom" => "London“, "United States" => "Washington"); $iterator = new Array. Iterator($cities); $iterator->rewind(); //start at begin of array // rewind to beginning of array while($iterator->valid()) { print $iterator->current(). " is in ". $iterator->key(). " <br/> "; $iterator->next(); Output: } London is in United Kingdom Washington is in United States

Try : Calculate Grade 18 $grades = array( 25, 64, 23, 87, 56, 38,

Try : Calculate Grade 18 $grades = array( 25, 64, 23, 87, 56, 38, 78, 57, 98, 95, 81, 67, 75, 76, 74, 82, 36, 39, 54, 43, 49, 65, 69, 78, 17, 91); ทำคำนวณ เกรด A>79, B>69, C>59, D>49, F<50 และแสดงเปนตาราง

Array in Form 19 <form method="post" action="array-form. php"> Select your favourite artists: <select name="artists[

Array in Form 19 <form method="post" action="array-form. php"> Select your favourite artists: <select name="artists[ ]" multiple="true"> <option value="Britney Spears">Britney Spears</option> <option value="Aerosmith">Aerosmith</option> <option value="Black-Eyed Peas">Black-Eyed Peas</option> </select> <p><input type="submit" name="submit" value="Submit" /> </form> สงอะเรย ในชอ $_POST['artists'] ทดลองสรางฟอรม รบขอมลแลวแสดงผลการเลอก

Array in Form 20 <ul> <? php if (isset($_POST['artists'])){ echo "You select: "; foreach

Array in Form 20 <ul> <? php if (isset($_POST['artists'])){ echo "You select: "; foreach ($_POST['artists'] as $t) { echo "<li>$t</li> rn"; } } ? > </ul>

Try: การเลอก Pizza 21 <form method="post" action="pizza. php"> Select your favourite pizza toppings: <br

Try: การเลอก Pizza 21 <form method="post" action="pizza. php"> Select your favourite pizza toppings: <br /> peppers</input> Before <input type="checkbox" name="toppings[]" value="olives">Olives</input> <input type="checkbox" name="toppings[]" value="mint">Mint</input> <input type="checkbox" name="toppings[]" value="bacon">Bacon</input> <p><input type="submit" name="submit" value="Submit" /></p> </form> After

Array Function 22

Array Function 22

ตวอยางการใชฟงกชน 23 <? php // define string $str = 'tinker, tailor, soldier, spy'; //

ตวอยางการใชฟงกชน 23 <? php // define string $str = 'tinker, tailor, soldier, spy'; // convert string to array // output: ('tinker', 'tailor', 'soldier, 'spy') $arr = explode(', ', $str); print_r($arr); ? > explode