Skip to content

Commit e6498e3

Browse files
committed
Initial commit
0 parents  commit e6498e3

8 files changed

+545
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
phpunit.xml
3+
composer.lock
4+
build
5+
vendor
6+
*.phar

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
7+
script: phpunit

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "rhumsaa/array_column",
3+
"description": "Provides functionality for array_column() to projects using PHP earlier than version 5.5.",
4+
"type": "library",
5+
"keywords": ["array", "column", "array_column"],
6+
"homepage": "https://github.com/ramsey/array_column",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Ben Ramsey",
11+
"homepage": "http://benramsey.com"
12+
}
13+
],
14+
"support": {
15+
"issues": "https://github.com/ramsey/array_column/issues",
16+
"source": "https://github.com/ramsey/array_column"
17+
},
18+
"autoload": {
19+
"files": ["src/array_column.php"]
20+
}
21+
}

phpunit.xml.dist

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
3+
<testsuites>
4+
<testsuite>
5+
<directory>./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory suffix=".php">./src</directory>
11+
</whitelist>
12+
</filter>
13+
<logging>
14+
<log type="coverage-html" target="build/coverage" title="Rhumsaa\Uuid"
15+
charset="UTF-8" yui="true" highlight="true"
16+
lowUpperBound="35" highLowerBound="70"/>
17+
<log type="coverage-clover" target="build/logs/clover.xml"/>
18+
<log type="junit" target="build/logs/junit.xml"
19+
logIncompleteSkipped="false"/>
20+
</logging>
21+
</phpunit>

src/array_column.php

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* This file is part of the array_column library
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
9+
* @license http://opensource.org/licenses/MIT MIT
10+
*/
11+
12+
if (!function_exists('array_column')) {
13+
14+
/**
15+
* Returns the values from a single column of the input array, identified by
16+
* the $columnKey.
17+
*
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+
* @param array $input A multi-dimensional array (record set) from which to pull
22+
* a column of values.
23+
* @param mixed $columnKey The column of values to return. This value may be the
24+
* integer key of the column you wish to retrieve, or it
25+
* may be the string key name for an associative array.
26+
* @param mixed $indexKey (Optional.) The column to use as the index/keys for
27+
* the returned array. This value may be the integer key
28+
* of the column, or it may be the string key name.
29+
* @return array
30+
*/
31+
function array_column($input = null, $columnKey = null, $indexKey = null)
32+
{
33+
// Using func_get_args() in order to check for proper number of
34+
// parameters and trigger errors exactly as the built-in array_column()
35+
// does in PHP 5.5.
36+
$params = func_get_args();
37+
38+
if (!isset($params[0])) {
39+
trigger_error('array_column() expects at least 2 parameters, 0 given', E_USER_WARNING);
40+
return null;
41+
} elseif (!isset($params[1])) {
42+
trigger_error('array_column() expects at least 2 parameters, 1 given', E_USER_WARNING);
43+
return null;
44+
}
45+
46+
if (!is_array($params[0])) {
47+
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
48+
return null;
49+
}
50+
51+
if (!is_int($params[1])
52+
&& !is_string($params[1])
53+
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
54+
) {
55+
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
56+
return false;
57+
}
58+
59+
if (isset($params[2])
60+
&& !is_int($params[2])
61+
&& !is_string($params[2])
62+
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
63+
) {
64+
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
65+
return false;
66+
}
67+
68+
$paramsInput = $params[0];
69+
$paramsColumnKey = (string) $params[1];
70+
$paramsIndexKey = (isset($params[2]) ? (string) $params[2] : null);
71+
$resultArray = array();
72+
73+
foreach ($paramsInput as $row) {
74+
75+
$key = $value = null;
76+
$keySet = $valueSet = false;
77+
78+
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
79+
$keySet = true;
80+
$key = $row[$paramsIndexKey];
81+
}
82+
83+
if (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
84+
$valueSet = true;
85+
$value = $row[$paramsColumnKey];
86+
}
87+
88+
if ($valueSet) {
89+
if ($keySet) {
90+
$resultArray[$key] = $value;
91+
} else {
92+
$resultArray[] = $value;
93+
}
94+
}
95+
96+
}
97+
98+
return $resultArray;
99+
}
100+
101+
}

0 commit comments

Comments
 (0)