Alternate to array_column()
Add your own function array_column
if you PHP version does not support it:
<?php
if (! function_exists('array_column')) {
function array_column(array $input, $columnKey, $indexKey = null) {
$array = array();
foreach ($input as $value) {
if ( !array_key_exists($columnKey, $value)) {
trigger_error("Key \"$columnKey\" does not exist in array");
return false;
}
if (is_null($indexKey)) {
$array[] = $value[$columnKey];
}
else {
if ( !array_key_exists($indexKey, $value)) {
trigger_error("Key \"$indexKey\" does not exist in array");
return false;
}
if ( ! is_scalar($value[$indexKey])) {
trigger_error("Key \"$indexKey\" does not contain scalar value");
return false;
}
$array[$value[$indexKey]] = $value[$columnKey];
}
}
return $array;
}
}
Reference:
Alternative to array_column function used as search
Add your own function array_column
if you PHP version does not support it:
if (!function_exists('array_column')) {
function array_column($input = null, $columnKey = null, $indexKey = null)
{
$argc = func_num_args();
$params = func_get_args();
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return null;
}
if (!is_array($params[0])) {
trigger_error(
'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
E_USER_WARNING
);
return null;
}
if (!is_int($params[1])
&& !is_float($params[1])
&& !is_string($params[1])
&& $params[1] !== null
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return false;
}
if (isset($params[2])
&& !is_int($params[2])
&& !is_float($params[2])
&& !is_string($params[2])
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return false;
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
if (isset($params[2])) {
if (is_float($params[2]) || is_int($params[2])) {
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}
}
Reference:
Is there any function in jQuery that is equivalent to PHP's array_column()?
You can do it with .map(). Immagine a fetch of database rows.
With arrow function
To have a reusable arrayColumn(array, column)
function:
const array = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
];
const arrayColumn = (array, column) => {
return array.map(item => item[column]);
};
const names = arrayColumn(array, 'name');
console.log(names);
What is the equivalent of array_column in python3
You can use list comprehension:
In [11]: [e['_source'] for e in data]
Out[11]: [{'id': 1, 'price': 100}, {'id': 2, 'price': 150}, {'id': 3, 'price': 90}]
array_column not working for an array of objects?
v7.0.0: Added the ability for the input parameter to be an array of objects.
Source: https://secure.php.net/manual/en/function.array-column.php
How do I get index of repeated data from multi dimension array in different variables using array_search() method
It is described in array_search
manual:
function Search($value, $array)
{
return array_keys($array, $value, false);
}
$array = array(45, 5, 1, 22, 22, 10, 10);
$value = "10";
$indexes = Search($value, $array);
print_r($indexes);
You can see full documentation of array_keys
here
php array_column with unsequential index returns wrong index
array_column() doesn't maintain indexes (although it allows you to set your own from other data columns in the row), but you can handle that using something like:
array_combine(
array_keys($myarray),
array_column($myarray,'columnbeingsearchedhere')
);
EDIT
Alternative, that probably grabs a bit more memory temporarily (unless you don't mind the original array being modified), but might be a bit faster overall (depending on your data):
$newArray = $myArray;
array_walk($newArray, function(&$value) use ($columnName) { $value = $value[$columnName]; } );
Isolate a single column in a multi-dimensional array
As of June 20th in PHP-5.5 there is a new function array_column
For example:
$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'
)
);
$firstNames = array_column($records, 'first_name');
print_r($firstNames);
Will return
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
There are even more examples in the above mentioned link.
Related Topics
Aescrypt Decryption Between iOS and PHP
How to Replace the Deprecated Set_Magic_Quotes_Runtime in PHP
Using Prepared Statement, How I Return the Id of the Inserted Row
How to Display Two Table Columns Per Row in PHP Loop
Unit Testing with Items That Need to Send Headers
Laravel Error: Method Illuminate\View\View::_Tostring() Must Not Throw an Exception
What Does [\S\S]* Mean in Regex in PHP
How to Retrieve the Visitor's Isp Through PHP
How Does {} Affect a MySQL Query in PHP
How to Check If a File Exists on a Remote Server Using PHP
PHP Eval and Capturing Errors (As Much as Possible)
How to Connect to Database on Another Server
Concat Columns with Laravel 5 Eloquent