Skip to content

Commit

Permalink
Add array_column()
Browse files Browse the repository at this point in the history
  • Loading branch information
sarciszewski committed Mar 8, 2015
1 parent 32f91df commit 217dc4a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Refer to http://php.net/eol.php for the latest updates on supported PHP versions
## Features polyfilled by this library

* PHP 5.5
* `array_column()`
* `boolval()`
* `hash_pbkdf2()`
* `openssl_pbkdf2()`
Expand Down
31 changes: 21 additions & 10 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@
});
error_reporting(E_ALL); ini_set('display_errors', 'On');


if (!function_exists('array_column')) {
/**
* Get the boolean value of a variable
*/
function array_column(array $array, $column_key, $index_key = null)
{
return Future\Utility::arrayColumn($array, $column_key, $index_key);
}
}

if (!function_exists('boolval')) {
/**
* Get the boolean value of a variable
*/
function boolval($mixed_var)
{
return !!$mixed_var;
}
}

if (!function_exists('hash_equals')) {
/**
* From PHP 5.6
Expand Down Expand Up @@ -107,13 +128,3 @@ function openssl_pbkdf2($password, $salt, $length, $iterations, $algo = 'sha1')
return $key;
}
}

if (!function_exists('boolval')) {
/**
* Get the boolean value of a variable
*/
function boolval($mixed_var)
{
return !!$mixed_var;
}
}
61 changes: 61 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace ResonantCore\PHPFuture;

/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Resonant Core, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
class Utility
{
/**
* Return the values from a single column in the input array
*
* @param array $array A multi-dimensional array (record set) from which to pull a column of values.
* @param string|int $column_key The column of values to return.
* @param string|int|null $index_key The column to use as the index/keys for the returned array.
*
* @return array
*/
public static function arrayColumn(array $array, $column_key, $index_key = null)
{
$aReturn = [];
if ($column_key === null) {
// No column key? Grab the whole row...
if ($index_key === null) {
return $array;
}
foreach ($array as $sub) {
$aReturn[$sub[$index_key]] = $sub;
}
} elseif (empty($index_key)) {
foreach ($array as $sub) {
$aReturn[] = $sub[$column_key];
}
} else {
foreach ($array as $sub) {
$aReturn[$sub[$index_key]] = $sub[$column_key];
}
}
return $aReturn;
}
}
28 changes: 28 additions & 0 deletions tests/Utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
use \ResonantCore\PHPFuture as Future;

class TestSecurity extends PHPUnit_Framework_TestCase
{
/**
* @covers \ResonantCore\PHPFuture\Utilty::arrayColumn()
*/
public function testArrayColumn()
{
$x = [
['a' => 1, 'b' => 1],
['a' => 2, 'b' => 3],
['a' => 3, 'b' => 2]
];

$y = [
1 => 1,
2 => 3,
3 => 2
];

$this->assertEquals(
$y,
Future\Utility::arrayColumn($x, 'b', 'a')
);
}
}
1 change: 0 additions & 1 deletion wishlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ PHP 5.5

FUNCTIONS
=========
array_column()
curl_escape()
curl_unescape()
pg_escape_literal()
Expand Down

0 comments on commit 217dc4a

Please sign in to comment.