1 / 24

4 Array

4 Array. อ.ธีระพล ลิ้มศรัทธา. ● array types ● foreach loop ● Use arrays with Web forms ● Built-in functions. การเก็บของอเรย์. <?php // define array $fruits = array('apple', 'banana', 'pineapple', 'grape'); ?> การเข้าถึง ใช้ อินเด็กซ์ทีเริ่มจากศูนย์ $fruits[0] $fruits[3].

arellanod
Download Presentation

4 Array

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4 Array อ.ธีระพล ลิ้มศรัทธา ● array types ● foreachloop ●Use arrays with Web forms ● Built-in functions

  2. การเก็บของอเรย์ <?php // define array $fruits = array('apple', 'banana', 'pineapple', 'grape'); ?> การเข้าถึง ใช้ อินเด็กซ์ทีเริ่มจากศูนย์ $fruits[0] $fruits[3]

  3. การใช้คีย์แทน อินเด็กซ์ <?php // define array $fruits = array('a' => 'apple‘,'b' => 'banana‘, 'p' => 'pineapple‘,'g' => 'grape’); ?> การเข้าถึง ใช้ $fruits['a']

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

  5. การกำหนดค่าอินเด็กซ์อาร์เรย์อัตโนมัติการกำหนดค่าอินเด็กซ์อาร์เรย์อัตโนมัติ ในกรณีที่ไม่ทราบว่าจะใช้อินเด็กซ์เท่าใด เราทำได้โดยการไม่เติมค่าเลขอินเด็กซ์ <?php // define array $cars[] = 'Ferrari'; $cars[] = 'Lamborghini'; ?>

  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' => 'secret', 'host' => '192.168.0.1' ); echo array_keys($data)[0]; //print username ?>

  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‘); // change 'ham' to 'turkey' $meats[2] = 'turkey'; ?>

  10. การลบค่าในอะเรย์ <?php // define array $meats = array( 'fish‘,'chicken‘,'ham‘,'lamb’); // remove 'fish' unset($meats[0]);// จะทำให้ณ อินเด็กซ์นี้เป็น Null ?>

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

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

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

  14. การอ่าน nested array • <?php • $phonebook = array( • array('Raymond','1234567','ray@pnet.in',), • array('Harold', '5942033','harold@tuff.com',) • ); • for ($i=0; $i<count($phonebook);$i++){ • echo $i ; • for($j = 0; $j<count($phonebook[$i]); $j++){ • echo $phonebook[$i][$j]; • } • echo '<br>'; • } • ?>

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

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

  17. ArrayIterator (New PHP5.0) $cities = array( "United Kingdom" => "London“,"United States" => "Washington"); $iterator = new ArrayIterator($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 

  18. Try : Calculate Grade $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, 69, 78, 17, 91); ทำคำนวณ เกรด A>79, B>69, C>59, D>49, F<50 และแสดงเป็นตาราง

  19. Array in Form <form method="post" action="array-form.php"> Select your favourite artists: <br /> <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']ทดลองสร้างฟอร์ม รับข้อมูลแล้วแสดงผลการเลือก

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

  21. Try: การเลือก Pizza Before <form method="post" action="pizza.php"> Select your favourite pizza toppings: <br /> peppers</input> <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

  22. Array Function

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

  24. สร้างการซุ่มรูปภาพ มีการสุ่มรูปทั้งสองรูป ทั้งมีคำบรรยายใต้รูปด้วย ให้ออกแบบที่มีสอง คอลัมน์ ในลักษณะไดนามิก (ใช้ฟังก์ชัน shuffle()) แม่รักลูก กระต่ายสีขาว

More Related