This article describes the simple implementation method of the array_column function in php. Share it for your reference, as follows:
array_column() in php can return the value of a single column in the input array。
Example:
<?php // Return array from the database:$a = array( array( 'id' => 0015, 'age' => '20', 'name' => 'Tom', ), array( 'id' => 0016, 'age' => '21', 'name' => 'Jack', ), array( 'id' => 0017, 'age' => '28', 'name' => 'Martin', ) ); $names = array_column($a, 'name'); print_r($names); /* Output: Array ( [0] => Tom [1] => Jack [2] => Martin )*/ ?>
Although the array_column function of php is very useful, the lower version does not have this function, so you can only implement one by yourself:
if (!function_exists("array_column")) { function array_column(array &$rows, $column_key, $index_key = null) { $data = array(); if (empty($index_key)) { foreach ($rows as $row) { $data[] = $row[$column_key]; } } else { foreach ($rows as $row) { $data[$row[$index_key]] = $row[$column_key]; } } return $data; } }
For more information about PHP related content, please check out the topic of this site:Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.