Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all values of a specific column in a multidimensional array [duplicate]

I have a multidimensional array, that has say, x number of columns and y number of rows.

I want specifically all the values in the 3rd column.

The obvious way to go about doing this is to put this in a for loop like this

for(i=0;i<y-1;i++)
{
   $ThirdColumn[] = $array[$i][3];
}

but there is an obvious time complexity of O(n) involved here. Is there a built in way for me to simply extract each of these rows from the array without having to loop in.

For example (this does not work offcourse)

$ThirdColumn  = $array[][3]
like image 734
Parijat Kalia Avatar asked Jun 18 '13 00:06

Parijat Kalia


People also ask

How can we get duplicate values in multidimensional array in PHP?

To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements.

How do you find the total number of elements in a multidimensional array?

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.

What is Array_column?

array_column() returns the values from a single column of the array , identified by the column_key . Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

What is multidimensional associative array?

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.


2 Answers

Given a bidimensional array $channels:

$channels = array(
    array(
        'id' => 100,
        'name' => 'Direct'
    ),
    array(
        'id' => 200,
        'name' => 'Dynamic'
    )
);

A nice way is using array_map:

$_currentChannels = array_map(function ($value) {
    return  $value['name'];
}, $channels);

and if you are a potentate (php 5.5+) through array_column:

$_currentChannels = array_column($channels, 'name');

Both results in:

Array
(
    [0] => Direct
    [1] => Dynamic
)

Star guests: array_map (php4+) and array_column (php5.5+)

// array array_map ( callable $callback , array $array1 [, array $... ] )
// array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] )
like image 124
Igor Parra Avatar answered Sep 30 '22 18:09

Igor Parra


Is there a built in way for me to simply extract each of these rows from the array without having to loop in.

Not yet. There will be a function soon named array_column(). However the complexity will be the same, it's just a bit more optimized because it's implemented in C and inside the PHP engine.

like image 33
hakre Avatar answered Sep 30 '22 19:09

hakre