13

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]
7
  • No, any iteration of this idea, will contain at least one for loop somewhere....its unavoidable Commented Jun 18, 2013 at 0:54
  • Post a print_r of your array Commented Jun 18, 2013 at 0:55
  • why do you need a print_r of my array ? Commented Jun 18, 2013 at 1:00
  • To see how your data is structured Commented Jun 18, 2013 at 1:02
  • It would be [2] for the third column, not [3]... Commented Jun 18, 2013 at 1:03

5 Answers 5

33

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 ] )
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

Try this....

   foreach ($array as $val)
   {
       $thirdCol[] = $val[2];
   }

Youll endup with an array of all values from 3rd column

1 Comment

this is off course, what I have stated in my post as the obvious way to go about doing it is by foreach looping, but thanks anyways
0

Another way to do the same would be something like $newArray = array_map( function($a) { return $a['desiredColumn']; }, $oldArray ); though I don't think it will make any significant (if any) improvement on the performance.

Comments

-2

You could try this:

$array["a"][0]=10;
$array["a"][1]=20;
$array["a"][2]=30;
$array["a"][3]=40;
$array["a"][4]=50;
$array["a"][5]=60;

$array["b"][0]="xx";
$array["b"][1]="yy";
$array["b"][2]="zz";
$array["b"][3]="aa";
$array["b"][4]="vv";
$array["b"][5]="rr";

$output = array_slice($array["b"], 0, count($array["b"]));

print_r($output);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.