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

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 นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สอนเขียน PHP แสดงการจองห้องประชุมแบบไฮไลท์ตามช่วงเวลา (แบบเชื่อมต่อฐานข้อมูล MySQL)

ตัวอย่าง ผลลัพธ์ที่ได้จากการจองในฐานข้อมูล ตาราง tb_room สร้างตารางรายชื่อห้องประชุม สำหรับ id นั้นเป็น Primarykey จะกำหนดให้สร้างอัตโนมัติ ทุกครั้งที่เราเพิ่มชื่อห้องประชุมใหม่ -- -- Database: `tobedev_example` -- -- -------------------------------------------------------- -- -- Table structure for table `tb_room` -- CREATE TABLE IF NOT EXISTS `tb_room` (   `id` int(11) NOT NULL,   `name` varchar(30) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- Dumping data for table `tb_room` -- INSERT INTO `tb_room` (`id`, `name`) VALUES (1, 'ห้องประชุม 1'), (2, 'ห้องประชุม 2'), (3, 'ห้องประชุม 3'), (4, 'ห้องประชุม 4'), (5, 'ห้องประชุม 5'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tb_room` -- ALTER TABLE `tb_room`   ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tb_room` -- ALTER TABLE `tb_room`   MODIFY `i...

การนำไอคอน มาแสดงบน Fullcalendar

เนื่องจากมีน้องคนหนึ่งให้ช่วยดูโค้ดเกี่ยวกับ Fullcalendar ให้หน่อย แล้วพอดีช่วงนี้ผมก็กำลังสนใจปฏิทิน Fullcalendar อยู่พอดี จึงลองหาสาเหตุที่ไม่สามารถแสดงข้อมูลจาก MySQL และไม่สามารถแทรก icon เข้าไปใน Fullcalendar ได้ จากที่สังเกตุได้ครั้งแรกคือ Error ที่ Console ของ Firefox เกี่ยวกับฟังก์ชั่นที่เขียนผิด และก็มีการ echo ค้างไว้ในส่วนของไฟล์ getCalendar.php ก็เลยจัดการทดสอบแล้วลบ echo ออกให้เหลือแค่ echo json_encode($event_array); ที่ได้ใช้งานจริงเท่านั้น ขั้นตอนการตรวจสอบความถูกต้องของโค้ด PHP 1) ต้องแน่ใจว่าคำสั่งที่เขียนไว้ สามารถดึงข้อมูลมาแสดงผลได้ด้วยการ echo $sql; 2) นำคำสั่งที่ได้ไปรันในโปรแกรมจัดการฐานข้อมูล ในที่นี้คือ phpMyAdmin 3) เมื่อตรวจสอบดูผลลัพธ์ที่ได้ หากถูกต้องมีข้อมูลก็แสดงว่าการ Query ทำงานได้ 4) มาดูการทำงานของ JavaScript ในส่วนของ jQuery มีการแจ้งเตือนที่ฟังก์ชั่น .on() ซึ่งจะใช้กับ jQuery เวอร์ชั่นใหม่เท่านั้น นั่นหมายถึงเวอร์ชั่น jQuery ที่มากับ Fullcalendar เป็นเวอร์ชั่นเก่า ก็ให้เปลี่ยนไปใช้ .live() แทน ก็จะทำงานได้ปกติ 5) ม...