Page 1 of 1

Multidimensional Array Help

Posted: Sun Dec 13, 2009 10:30 am
by JakeJ
I specifically want to access the first element of the first and last row in a multidimensional array. I cannot seem to figure out a good way to do this.

Any help would be appreciated.

Thanks!

Jake

Re: Multidimensional Array Help

Posted: Sun Dec 13, 2009 1:20 pm
by McInfo

Code: Select all

<?php
// Numeric
$array = array
(   0 => array
    (   0 => 'R'
    ,   1 => 'S'
    ,   2 => 'T'
    )
,   1 => array
    (   0 => 'U'
    ,   1 => 'V'
    ,   2 => 'W'
    )
,   2 => array
    (   0 => 'X'
    ,   1 => 'Y'
    ,   2 => 'Z'
    )
);
 
// Works with numeric arrays
echo $array[0][0]; // R
echo $array[count($array) - 1][0]; // X
 
// Associative
$array = array
(   'zero' => array
    (   'a' => 'R'
    ,   'b' => 'S'
    ,   'c' => 'T'
    )
,   'one' => array
    (   'a' => 'U'
    ,   'b' => 'V'
    ,   'c' => 'W'
    )
,   'two' => array
    (   'a' => 'X'
    ,   'b' => 'Y'
    ,   'c' => 'Z'
    )
);
 
// Works with numeric and associative arrays
$first = reset($array);
echo reset($first); // R
 
$last = end($array);
echo reset($last); // X
Edit: This post was recovered from search engine cache.