Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > มาใช้ฟังก์ชั่นใหม่ array_column() ที่เพิ่งคลอดใน PHP5.5 กันเถอะ



 
Clound SSD Virtual Server

มาใช้ฟังก์ชั่นใหม่ array_column() ที่เพิ่งคลอดใน PHP5.5 กันเถอะ


มาใช้ฟังก์ชั่นใหม่ array_column() ที่เพิ่งคลอดใน PHP5.5 กันเถอะ วันนี้ (21 มี.ค. 56) ได้เข้าไปเว็บ php.net และเห็นว่า PHP5.5 เข้าสู่ระยะ beta แล้ว และในลิสต์ของฟีเจอร์ก็มีฟังก์ชั่น array_column() อยู่ในนั้น
บทความนี้จะอธิบายว่าฟังก์ชั่นนี้ใช้ทำอะไร และจะเขียนฟังก์ชั่นนี้เพื่อไปใช้กับ PHP เวอร์ชั่นอื่นอย่างไร




ฟังก์ชั่นนี้ถูกเสนอโดยคณะผู้พัฒนา PHP มาระยะหนึ่งแล้ว ตามลิงก์นี้
https://wiki.php.net/rfc/array_column (ตัวอย่างในบทความนี้ก็ใช้โค้ดจากลิงก์ดังกล่าว)

มีประโยชน์คือ "สร้าง array ของสมาชิกใน array หลายมิติ ตาม index หรือ key ที่กำหนด"

รูปแบบ
array array_column(array $input, mixed $columnKey[, mixed $indexKey])


$input - array หลายมิติ ที่เราจะใช้อ่านข้อมูล
$columnKey - ชื่อคอลัมน์ที่จะอ่านข้อมูล สามารถเป็นทั้ง index สำหรับ indexed array และ key สำหรับ associative array
$indexKey (ไม่กำหนดก็ได้) - ชื่อคอลัมน์ที่จะเป็น index หรือ key สำหรับ array ที่จะสร้าง




ตัวอย่างที่ 1 - สร้าง array ที่มีประกอบไปด้วยค่าของสมาชิกชื่อ first_name
<?php $records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe' ), array( 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith' ), array( 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones' ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe' ) ); // หากเราต้องการสร้าง array ที่มีแต่ค่าของ first_name เท่านั้น $firstNames = array_column($records, 'first_name'); print_r($firstNames);


ผลลัพธ์
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )


ซึ่งโดยปกติถ้าเราต้องการผลลัพธ์อย่างข้างบนเราต้องวนลูปเพื่อสร้าง array ใหม่เองแบบนี้

<?php // ใช้ข้อมูล array $records จากตัวอย่างที่ 1 // หากเราต้องการสร้าง array ที่มีแต่ค่าของ first_name เท่านั้น // ต้องสร้าง array ว่างๆ ขึ้นมาก่อน $firstNames = array(); // วนลูปสมาชิกทุกตัวของ $records foreach ($records as $record) { // เพิ่มค่า first_name ของสมาชิกแต่ละตัวเข้าไปใน array ที่เราสร้างไว้ก่อนหน้า $firstNames[] = $record['first_name']; } print_r($firstNames);


ซึ่งหากต้องมีการทำแบบนี้หลายๆ ที่ในโปรแกรมของเราคงไม่ดีแน่
ฟังก์ชั่น array_column() จึงเกิดมาเพื่อเหตุผลนี้




ตัวอย่างที่ 2 - สร้าง array ของสมาชิกใน indexed array
<?php $records = array( // [0] [1] [2] array(1, 'John', 'Doe'), array(2, 'Sally', 'Smith'), array(3, 'Jane', 'Jones') ); // เลือกเฉพาะสมาชิก index 2 (ลำดับที่ 3) $lastNames = array_column($records, 2); print_r($lastNames);


ผลลัพธ์
Array ( [0] => Doe [1] => Smith [2] => Jones )





ตัวอย่างที่ 3 - สร้าง array ของ last_name และให้ค่าของ id เป็น key
<?php // ใช้ข้อมูล array $records จากตัวอย่างที่ 1 $lastNames = array_column($records, 'last_name', 'id'); print_r($lastNames);


ผลลัพธ์
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )





เนื่องจากฟังก์ชั่นนี้เพิ่งถูกเสนอและเพิ่งถูกเพิ่มเข้าไปใน PHP5.5
ดังนั้นแน่นอนว่าคงไม่สามารถใช้ฟังก์ชั่นนี้ในเครื่องเซิร์ฟเวอร์ทั่วไป (เพราะส่วนใหญ่ยังเป็นเวอร์ชั่น 5.2 อยู่เลย)
แต่ผมได้เขียนฟังก์ชั่นนี้ไว้ใช้ทดแทนกัน
ความเร็วในการทำงานก็แน่นอนว่าคงสู้ตัวที่อยู่ในเวอร์ชั่น 5.5 ไม่ได้ (เพราะเขาเขียนด้วยภาษา C)
แต่ก็สามารถทำให้ใช้ฟังก์ชั่นนี้ไปก่อนได้โดยไม่ต้องรอ 5.5
ตัวโค้ดอาจจะดูเหมือนว่าสามารถเขียนให้สั้นกว่านี้ได้ เพราะมีหลายจุดที่ซ้ำซ้อน
แต่จริงๆ แล้วเป็นการ optimize ให้ทำงานเร็วที่สุดเท่าที่จะเป็นไปได้

array_column.php
<?php // หากไม่มีฟังก์ชั่น array_column ถูกกำหนดไว้อยู่แล้ว if (!function_exists('array_column')) { // ประกาศเองเลย function array_column(array $input, $columnKey, $indexKey = null) { // array ที่จะส่งคืนกลับไป $output = array(); // ทำให้ $columnKey เป็นชนิด string $columnKey = (string)$columnKey; // ตรวจว่ามีการกำหนด $indexKey หรือไม่ if (isset($indexKey)) { // ทำให้ $indexKey เป็นชนิด string $indexKey = (string)$indexKey; // หาก $indexKey และ $columnKey มีค่าเดียวกัน if ($indexKey === $columnKey) { foreach ($input as $key => $item) { // หาก $item ไม่ใช่ array หรือ // หากไม่มี $columnKey อยู่ใน $item if (!is_array($item) || !array_key_exists($columnKey, $item)) { // อ่านแถวถัดไปเลย continue; } // เราไม่จำเป็นต้องตรวจว่ามี $indexKey อยู่ใน $item หรือไม่ // เพราะ $indexKey มีค่าเดียวกันกับ $columnKey $output[$item[$indexKey]] = $item[$columnKey]; } } // หาก $indexKey และ $columnKey มีค่าไม่เหมือนกัน else { foreach ($input as $key => $item) { // หาก $item ไม่ใช่ array หรือ // หากไม่มี $columnKey อยู่ใน $item if (!is_array($item) || !array_key_exists($columnKey, $item)) { // อ่านแถวถัดไปเลย continue; } // หากมี $indexKey อยู่ใน $item if (array_key_exists($indexKey, $item)) { // ใช้ค่าของ $item[$indexKey] เป็น key $output[$item[$indexKey]] = $item[$columnKey]; } // แต่หากไม่มี $indexKey อยู่ใน $item else { // ให้เป็น indexed array $output[] = $item[$columnKey]; } } } } // หากไม่ได้กำหนด $indexKey else { foreach ($input as $key => $item) { // หาก $item ไม่ใช่ array หรือ // หากไม่มี $columnKey อยู่ใน $item if (!is_array($item) || !array_key_exists($columnKey, $item)) { // อ่านแถวถัดไปเลย continue; } // ให้เป็น indexed array ไปทั้งหมด $output[] = $item[$columnKey]; } } return $output; } }

ฟังก์ชั่นที่ปรากฏในบทความนี้ (4)







   
Share
Bookmark.   

  By : phpinfo()
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2013-03-21
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่