|
| 1 | +# array_column() for PHP |
| 2 | + |
| 3 | +[](https://travis-ci.org/ramsey/array_column) |
| 4 | + |
| 5 | +This simple library provides functionality for [`array_column()`](https://wiki.php.net/rfc/array_column) |
| 6 | +to versions of PHP earlier than 5.5. It mimics the functionality of the built-in |
| 7 | +function in every way. |
| 8 | + |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +``` |
| 13 | +array array_column(array $input, mixed $columnKey[, mixed $indexKey]) |
| 14 | +``` |
| 15 | + |
| 16 | +Given a multi-dimensional array of data, `array_column()` returns the values |
| 17 | +from a single column of the input array, identified by the `$columnKey`. |
| 18 | +Optionally, you may provide an `$indexKey` to index the values in the returned |
| 19 | +array by the values from the `$indexKey` column in the input array. |
| 20 | + |
| 21 | +For example, using the following array of data, we tell `array_column()` to |
| 22 | +return an array of just the last names, indexed by their record IDs. |
| 23 | + |
| 24 | +``` php |
| 25 | +<?php |
| 26 | +$records = array( |
| 27 | + array( |
| 28 | + 'id' => 2135, |
| 29 | + 'first_name' => 'John', |
| 30 | + 'last_name' => 'Doe' |
| 31 | + ), |
| 32 | + array( |
| 33 | + 'id' => 3245, |
| 34 | + 'first_name' => 'Sally', |
| 35 | + 'last_name' => 'Smith' |
| 36 | + ), |
| 37 | + array( |
| 38 | + 'id' => 5342, |
| 39 | + 'first_name' => 'Jane', |
| 40 | + 'last_name' => 'Jones' |
| 41 | + ), |
| 42 | + array( |
| 43 | + 'id' => 5623, |
| 44 | + 'first_name' => 'Peter', |
| 45 | + 'last_name' => 'Doe' |
| 46 | + ) |
| 47 | +); |
| 48 | + |
| 49 | +$lastNames = array_column($records, 'last_name', 'id'); |
| 50 | +``` |
| 51 | + |
| 52 | +If we call `print_r()` on `$lastNames`, you'll see a resulting array that looks |
| 53 | +a bit like this: |
| 54 | + |
| 55 | +``` text |
| 56 | +Array |
| 57 | +( |
| 58 | + [2135] => Doe |
| 59 | + [3245] => Smith |
| 60 | + [5342] => Jones |
| 61 | + [5623] => Doe |
| 62 | +) |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +## Installation |
| 67 | + |
| 68 | +The easiest way to install this library is to use Composer and add the following |
| 69 | +to your project's `composer.json` file: |
| 70 | + |
| 71 | +``` javascript |
| 72 | +{ |
| 73 | + "require": { |
| 74 | + "rhumsaa/array_column": "~1.0.0" |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Then, when you run `composer install`, everything will fall magically into place, |
| 80 | +and the `array_column()` function will be available to your project, as long as |
| 81 | +you are including Composer's autoloader. |
| 82 | + |
| 83 | +_However, you do not need Composer to use this library._ |
| 84 | + |
| 85 | +This library has no dependencies and should work on older versions of PHP. |
| 86 | +Download the code and include `src/array_column.php` in your project, and all |
| 87 | +should work fine. |
| 88 | + |
| 89 | +When you are ready to run your project on PHP 5.5, everything should |
| 90 | +continue to run well without conflicts, even if this library remains included |
| 91 | +in your project. |
0 commit comments