ข้ามไปที่เนื้อหาหลัก

PHP : Array Function เรียนรู้และใช้งานฟังก์ชั่นที่ใช้กับอาร์เรย์


สำหรับการเขียนโปรแกรม PHP นั้น ส่วนใหญ่ก็คือการจัดการกับข้อมูลจากฐานข้อมูล MySQL ดังนั้นการเพิ่งแต่คิวรี่จาก SQL อย่างเดียวก็ดูเหมือนจะยกภาระให้กับ MySQL Server เยอะเกินไป

การนำฟังก์ชั่นเกี่ยวกับอาร์เรย์มาช่วยจัดการข้อมูลต่างๆนั้น จึงเป็นวิธีหนึ่งที่ช่วยลดภาระของ MySQL และยังทำให้การทำงานบางส่วนของโปรแกรมเร็วขึ้นได้อีกด้วย

ซึ่งจะเหมาะอย่างยิ่งกับการจัดการข้อมูลอื่นๆที่ไม่ได้มาจากฐานข้อมูล MySQL ได้อย่างดีอีกด้วย เช่น ข้อมูลที่อ่านมาจาก Text File หรือ Import Excel เข้ามาใช้งาน

สำหรับฟังก์ชั่นที่ทำงานเกี่ยวกับอาร์เรย์มีดังนี้
  • array_change_key_case —เปลี่ยนคีย์ของอาร์เรย์ให้เป็นตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่
  • array_chunk — แยกอาร์เรย์ออกเป็นชุดๆ (เหมือนการแยกเป็นแถวๆ ระบุจำนวนคอลัมน์)
  • array_column — ดึงค่าจากคอลัมน์ใดคอลัมน์หนึ่ง จากทุกแถว ออกมาเป็นอาร์เรย์ชุดใหม่
  • array_combine — สร้างอาร์เรย์ชุดใหม่ โดยนำอาร์เรย์ชุดแรก สร้างเป็น key อาร์เรย์ชุดที่สองสร้างเป็น value
  • array_count_values —นับจำนวนอาร์เรย์ที่เหมือนกัน สร้างเป็นอาร์เรย์ชุดใหม่ นำค่าที่นับเป็น key และจำนวนที่นับได้เป็น value
  • array_diff_assoc — คัดอาร์เรย์ชุดแรก ที่ไม่มีอยู่ในอาร์เรย์ชุดอื่นๆ (โดยเปรียบเทียบที่ value)
  • array_diff_key — คัดอาร์เรย์ชุดแรก ที่ไม่มีอยู่ในอาร์เรย์ชุดอื่นๆ (โดยเปรียบเทียบที่ key)
  • array_diff_uassoc — คัดอาร์เรย์ชุดแรก เปรียบเทียบอาร์เรย์ชุดอื่นๆ  โดยกำหนดฟังก์ชั่นเพื่อสร้างเงื่อนไขที่จะตรวจสอบเองได้ (โดยเปรียบเทียบที่ value)
  • array_diff_ukey — คัดอาร์เรย์ชุดแรก เปรียบเทียบอาร์เรย์ชุดอื่นๆ  โดยกำหนดฟังก์ชั่นเพื่อสร้างเงื่อนไขที่จะตรวจสอบเองได้ (โดยเปรียบเทียบที่ key)
  • array_diff — คัดกรองอาร์เรย์ชุดแรก ที่ไม่มีในอาร์เรย์อื่นๆ (โดยเปรียบเทียบที่ value)
  • array_fill_keys —ใช้สำหรับเติมข้อมูลที่กำหนดลงไปในอาร์เรย์ อาร์เรย์แรกจะใช้เป็น key และกำหนดค่าเดียวกันให้กับคีย์ทั้งหมดนี้ หรือถ้าพารามิเตอร์ตัวที่ 2 ก็เป็นอาร์เรย์ จะทำงานเหมือน array_combine
  • array_fill —ใช้สำหรับเติมข้อมูลที่กำหนดลงไปในอาร์เรย์ตามช่วงที่กำหนด
  • array_filter —ใช้กรองข้อมูลที่มีค่าเป็นจริง และไม่ใช่ 0 หรือสามารถกำหนดฟังก์ชั่นที่ต้องการใช้ตรวจสอบเงื่อนไขที่ต้องการได้เอง
  • array_flip —สลับตำแหน่งระหว่าง key กับ value
  • array_intersect_assoc —เลือกเฉพาะอาร์เรย์ที่ตรงกันทั้ง key และ value
  • array_intersect_key —เลือกเฉพาะอาร์เรย์ที่มี key ตรงกัน
  • array_intersect_uassoc — เลือกเฉพาะอาร์เรย์ที่ key และ value ตรงกัน โดยสามารถกำหนดฟังก์ชั่นให้กับ value และ key ก่อนเปรียบเทียบได้ด้วย
  • array_intersect_ukey —เลือกเฉพาะอาร์เรย์ที่ key ตรงกันโดยกำหนดเงื่อนไขเพื่อตรวจสอบข้อมูล key ของแต่ละตัวได้เอง
  • array_intersect —เลือกเฉพาะอาร์เรย์ที่มี value ตรงกันกัน
  • array_key_exists —ตรวจสอบว่าอาร์เรย์ชุดนั้นๆ มี key ที่ต้องการหรือไม่
  • array_keys — คืนค่า key ทั้งหมดของอาร์เรย์ที่ต้องการ เป็นอาร์เรย์ชุดใหม่
  • array_map — กำหนดฟังก์ชั่นที่ต้องการใช้กับอาร์เรย์ชุดนั้นๆ
  • array_merge_recursive —นำอาร์เรย์หลายๆชุดมาผสานเข้าด้วยกันแบบหลายมิติ
  • array_merge — นำอาร์เรย์หลายๆชุดมาผสานเข้าด้วยกันแบบมิติเดียว
  • array_multisort — เรียงลำดับข้อมูลแบบหลายๆข้อมูล เช่นจัดเรียงข้อมูลที่ได้จากฐานข้อมูล
  • array_pad — แทรกอาร์เรย์ตามจำนวนและค่าที่กำหนด
  • array_pop — ตัดอาร์เรย์ค่าสุดท้ายออกจากตัวแปรอาร์เรย์ที่กำหนด
  • array_product —การนำค่าอาร์เรย์มาคูณต่อเนื่องกัน
  • array_push —การแทรกค่าใหม่เข้าไปในอาร์เรย์เป็นค่าสุดท้าย
  • array_rand — สุ่มค่าจากอาร์เรย์ที่กำหนด สามารถระบุจำนวนค่าของอาร์เรย์ที่ต้องการได้
  • array_reduce —เป็นการคืนค่าออกจากฟังก์ชั่นที่เรียกใช้งานเพื่อการคำนวณ
  • array_replace_recursive — แทนที่อาร์เรย์แบบหลายมิติ จะเริ่มแทนที่ตำแหน่งแรก
  • array_replace — Replaces elements from passed arrays into the first array
  • array_reverse — Return an array with elements in reverse order
  • array_search — Searches the array for a given value and returns the corresponding key if successful
  • array_shift — Shift an element off the beginning of array
  • array_slice — Extract a slice of the array
  • array_splice — Remove a portion of the array and replace it with something else
  • array_sum — Calculate the sum of values in an array
  • array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_udiff — Computes the difference of arrays by using a callback function for data comparison
  • array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_uintersect — Computes the intersection of arrays, compares data by a callback function
  • array_unique — Removes duplicate values from an array
  • array_unshift — Prepend one or more elements to the beginning of an array
  • array_values — Return all the values of an array
  • array_walk_recursive — Apply a user function recursively to every member of an array
  • array_walk — Apply a user supplied function to every member of an array
  • array — Create an array
  • arsort — Sort an array in reverse order and maintain index association
  • asort — Sort an array and maintain index association
  • compact — Create array containing variables and their values
  • count — Count all elements in an array, or something in an object
  • current — Return the current element in an array
  • each — Return the current key and value pair from an array and advance the array cursor
  • end — Set the internal pointer of an array to its last element
  • extract — Import variables into the current symbol table from an array
  • in_array — Checks if a value exists in an array
  • key_exists — Alias of array_key_exists
  • key — Fetch a key from an array
  • krsort — Sort an array by key in reverse order
  • ksort — Sort an array by key
  • list — Assign variables as if they were an array
  • natcasesort — Sort an array using a case insensitive "natural order" algorithm
  • natsort — Sort an array using a "natural order" algorithm
  • next — Advance the internal array pointer of an array
  • pos — Alias of current
  • prev — Rewind the internal array pointer
  • range — Create an array containing a range of elements
  • reset — Set the internal pointer of an array to its first element
  • rsort — Sort an array in reverse order
  • shuffle — Shuffle an array
  • sizeof — Alias of count
  • sort — Sort an array
  • uasort — Sort an array with a user-defined comparison function and maintain index association
  • uksort — Sort an array by keys using a user-defined comparison function
  • usort — Sort an array by values using a user-defined comparison function

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย PHP สำหรับนำไปใช้ในงานต่างๆ

       สำหรับหลายท่านที่ขอโค้ดเข้ามาทาง Inbox ของเฟซบุ๊กแฟนเพจ หรือถามถึงระบบต่างๆหลังไมค์มานั้น ส่วนใหญ่ก็มีแจกอยู่แล้วในเว็บบอร์ด ThaiCreate.Com นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สร้างแบบฟอร์มล็อกอิน HTML + PHP

       ผมจะสร้างระบบตัวอย่างเพื่อแสดงให้เห็นการทำงานของสคริปต์ PHP ที่ใช้ในการล็อกอินอย่างง่ายๆ นะครับ โดยที่ผมจะสร้างโฟลเดอร์ใหม่ขึ้นในไดร์ฟที่เราได้กำหนดให้เป็น Directory Root สำหรับรันสคริปต์ PHP ซึ่งผมกำหนดเอาไว้ที่ D:\www ดังนั้นผมจะสร้างโฟลเดอร์ teacher_assistant ขึ้นมาใหม่จะได้พาธเป็น D:\www\teacher_assistant สำหรับการเข้าถึง จะใช้ URL ดังนี้ http://localhost/teacher_assistant หลังจากสร้างโฟลเดอร์เสร็จเรียบร้อยก็จะเริ่มกระบวนการเตรียมโครงสร้างโปรเจ็กต์ด้วยเครื่องมือช่วยเขียนโปรแกรมชื่อว่า eclipse ซึ่งดูวิธีการสร้างโปรเจ็กต์ได้จากบทความ เริ่มต้นสร้างโปรเจ็กต์ ด้วยโปรแกรม eclipse เครื่องมือเขียนโปรแกรม php ในบทความเป็นการสร้างโปรเจ็กต์ที่มีพาธเป็นโฟลเดอร์ login แต่ในที่นี้ต้องเป็นพาธให้ตรงกับโฟลเดอร์ใหม่ที่เราได้สร้างขึ้นด้วยนะครับ

PHP กับการคิดส่วนลดเป็นเปอร์เซ็น 5%, 10%, 20% ตามช่วงราคาที่กำหนด

<?php     $total_price = 1000;     $discount = 0;         if($total_price >= 500 && $total_price < 1001){         $discount = 5;     }elseif($total_price >= 1001 && $total_price <= 5000){         $discount = 10;     }elseif($total_price >= 5001){         $discount = 20;     }         $discount_bath = ($total_price*$discount)/100; ?> <pre> ซื้อสินค้าครบ 0 ถึง 499 บาท ไม่ได้ส่วนลด ซื้อสินค้าครบ 500 ถึง 1000 บาท ได้ส่วนลด 5% ซื้อสินค้าครบ 1001 ถึง 5000บาท ได้ส่วนลด 10% ซื้อสินค้าครบ 5001 บาทขึ้นไป ได้ส่วนลด 20% </pre> <h3>รวมราคาสินค้า = <?php echo number_format($total_price,2);?></h3> <h5>ส่วนลด = <?php echo $discount;?>%  (<?php echo $discount_bath;?> บาท)</h5> <h2>ราคาหลังส่วนลด = <?php echo number_format($total_price - $discount_bath,2);?>บาท</h2> "PHP ไม่ได้สร้างสุดยอดโปรแกรม แต่ PHP ช่วยให้งานคุณง่ายขึ้นต