2

I have a bidimensional PHP array (matrix).

Is there anyway to extract (echo or save into another variable) a line or a column from this matrix without iterating through the elements?

Suppose we have this matrix:

A A A B
A A A C
A B C D

I want to do something like:

display_and_cut_first_line()

//Matrix after this step:    
A A A C
A B C D

display_and_cut_last_column()
//Matrix after this step:
A A A
A B C

It only has to work for marginal elements (first/last line, first/last column). I was thinking of somehow using slice, but didn't manage to do it.

1
  • Upgrade to PHP5.5 as soon as it's available, and get access to the array_column() function - benramsey.com/blog/2013/03/… Commented Jun 7, 2013 at 8:39

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Think you might mean echo array_pop($a); as OP wants to echo the values cut.
If you can get it to return the removed section, like array_pop et al would, that would be even better, I would have thought.
@IMSoP: Finished and added a live example. Works like you describe.
@IMSoP: Thank you. I think I produced something neater yesterday, you might find it amusing.
Great, but here comes a problem: I do this until the matrix is empty. The problem is that this will not work if the array is uni-dimensional (array_walk will iterate through integer values and try to use array_pop on those, resulting in an error). How can I check if an array is uni-dimensional? (is one line or one column only)
|
0

Yes. To get the first row:

$arr[0];

To get the last column:

$keys = array_keys($arr);
array_column($arr, end($keys));

1 Comment

I'd never heard of array_column(), which isn't surprising, as it's only introduced in PHP 5.5, which (still!) isn't officially a stable release yet. Interesting to know for future reference though...
0
<?php 
$example = array();
$example[0][0] = 'A';$example[0][1] = 'A';$example[0][2] = 'A';$example[0][3] = 'B';
$example[1][0] = 'A';$example[1][1] = 'A';$example[1][2] = 'A';$example[1][3] = 'C';
$example[2][0] = 'A';$example[2][1] = 'B';$example[2][2] = 'C';$example[2][3] = 'D';

$example = display_and_cut_first_line($example);
print_r($example);
/* Array
(
    [0] => Array
        (
            [0] => A
            [1] => A
            [2] => A
            [3] => C
        )

    [1] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
        )

)
 */
$example = display_and_cut_last_column($example);
print_r($example);
/* Array
(
    [0] => Array
        (
            [0] => A
            [1] => A
            [2] => A
        )

    [1] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

)
 */
function display_and_cut_first_line($array){
    array_shift($array);
    return ($array);
}

function display_and_cut_last_column($array){
    $result = array();
    foreach($array as $data):
        array_pop($data);
        $result[] = $data;
    endforeach;

    return $result;
}

?>

Comments

-2

Partly.

Cutting out a line can be done, using array_splice().

A "column" is not a recognisable entry in the array implementation, so you have to at least iterate the rows, possibly using array_map().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.