Is There a Function to Extract a 'Column' from an Array in PHP

Is there a function to extract a 'column' from an array in PHP?

Why does it have to be a built in function? No, there is none, write your own.

Here is a nice and easy one, as opposed to others in this thread.

$namearray = array();

foreach ($array as $item) {
$namearray[] = $item['name'];
}

In some cases where the keys aren't named you could instead do something like this

$namearray = array();

foreach ($array as $key => $value) {
$namearray [] = $value;
}

Is there any PHP array_* function for extracting single values from a multidimensional array?

Try array_column function;

array_column(array $data, 'key')

Extract line or column from PHP array

Extracting lines is easy: array_pop (last line) and array_shift (first line) do this for you.

Extracting columns is very easy on PHP 5.5 with array_column. For earlier versions it would have to be done manually with array_map or array_walk and the pop/shift functions, operating on each line in turn:

function extract_last_column(&$array) {
$column = array();
array_walk($array, function(&$row) use(&$column) {
$column[] = array_pop($row);
});
return $column;
}

See it in action.

If desired you can generalize this by using array_slice instead of array_pop and array_shift -- but for these particular operations it will be slower.

How to extract particular fields from an array

You can use for example array_map, but why do you not want to use Loops? In fact every method, that is able to modify the array in the way you want it, will iterate over it.

function reduce_to_title ($item) {
return $item['title'];
};
$titles = array_map('reduce_to_title', $articles);

Or since PHP>=5.3

$titles = array_map(function ($item) {
return $item['title'];
}, $articles);

How to extract data from column and insert into an array and add the values

Setting aside the comma-delimeted data being an anti-pattern, it's possible to return your aggregated values in a single query without needing to loop over anything by using string_split, the basics of which are:

select
Sum(case col when 1 then v end),
Sum(case col when 2 then v end),
Sum(case col when 3 then v end),
Sum(case col when 4 then v end)
from (
select v, row_number() over(partition by id order by id) col
from t
cross apply(
select Cast(value as int)v from String_Split(t.hours,',')
)v
)v

demo Fiddle

Extract column from csv data in PHP

If this a comma delimited string, you could use str_getcsv() in this case:

// load the string and explode, then apply str_getcsv
$data = array_map('str_getcsv', explode("\n", $request->getResponseBody()));
array_shift($data); // remove the header
$fourth_column = array();
foreach($data as $line) {
if(!empty($line[3])) {
$fourth_column[] = $line[3]; // indices start at zero
}
}

PHP - Extracting a property from an array of objects

If you have PHP 5.5 or later, the best way is to use the built in function array_column():

$idCats = array_column($cats, 'id');

But the son has to be an array or converted to an array

Extracting keys (stored in child array) from an array

You can use array_column to get values of a single column from an array

$array = [
['key' => 'foo', 'value' => 'fooVal'],
['key' => 'bar', 'value' => 'barVal'],
];

$result = array_column( $array , 'key' );

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

Array
(
[0] => foo
[1] => bar
)

Doc: array_column()



Related Topics



Leave a reply



Submit