Simple implementation method of Array_column function in PHP _php skills

Source: Internet
Author: User

This article illustrates the simple implementation method of Array_column function in PHP. Share to everyone for your reference, specific as follows:

array_column () in PHP returns the value of a single column in the input array .

Example:

<?php
//Returns an array from the database:
$a = array (
  ' id ' => 0015, ' Age
  ' => ', '
  name ' => ' Tom ') ,
 ),
 array (
  ' id ' => 0016,
  ' age ' => ', '
  name ' => ' Jack ',
 ),
 Array (
  ' id ' => 0017,
  ' age ' => ',
  ' name ' => ' Martin ',)
;
$names = Array_column ($a, ' name ');
Print_r ($names);
/*
output:
Array
(
 [0] => Tom
 [1] => Jack
 [2] => Martin
) * *
?>

Although PHP's Array_column function works very well, but the lower version does not have this function, then it is only possible to implement one for yourself:

if (!function_exists ("Array_column")) {
  function Array_column (array & $rows, $column _key, $index _key = null) { c2/> $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 interested readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic grammar Introductory Course" , "PHP Operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "Php+mysql database operation Introductory Course" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.