Giter Site home page Giter Site logo

array_column's Introduction

array_column() for PHP

Build Status Coverage Status Latest Stable Version Latest Unstable Version Total Downloads HHVM Status

This simple library provides functionality for array_column() to versions of PHP earlier than 5.5. It mimics the functionality of the built-in function in every way.

Usage

array array_column(array $input, mixed $columnKey[, mixed $indexKey])

Given a multi-dimensional array of data, array_column() returns the values from a single column of the input array, identified by the $columnKey. Optionally, you may provide an $indexKey to index the values in the returned array by the values from the $indexKey column in the input array.

For example, using the following array of data, we tell array_column() to return an array of just the last names, indexed by their record IDs.

<?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'
    )
);

$lastNames = array_column($records, 'last_name', 'id');

If we call print_r() on $lastNames, you'll see a resulting array that looks a bit like this:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

Installation

The easiest way to install this library is to use Composer and add the following to your project's composer.json file:

{
    "require": {
        "rhumsaa/array_column": "~1.1"
    }
}

Then, when you run composer install, everything will fall magically into place, and the array_column() function will be available to your project, as long as you are including Composer's autoloader.

However, you do not need Composer to use this library.

This library has no dependencies and should work on older versions of PHP. Download the code and include src/array_column.php in your project, and all should work fine.

When you are ready to run your project on PHP 5.5, everything should continue to run well without conflicts, even if this library remains included in your project.

array_column's People

Contributors

ramsey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.