Alex Rivera | Logout

Access PHP array element with a function?

Asked 2008-12-28T18:54:30.570
15

I'm working on a program that uses PHP's internal array pointers to iterate along a multidimensional array. I need to get an element from the current row, and I've been doing it like so:

$arr[key($arr)]['item']

However, I'd much prefer to use something like:

current($arr)['item'] // invalid syntax

I'm hoping there's a function out there that I've missed in my scan of the documentation that would enable me to access the element like so:

getvalue(current($arr), 'item')

or

current($arr)->getvalue('item')

Any suggestions?

Edit
Report

1 Answer

-1

If this does not work, how is your multidimensional array composed? A var_dump() might help.

$subkey = 'B';
$arr = array(
    $subkey => array(
        'AB' => 'A1',
        'AC' => 'A2'
    )
);


echo current($arr[$subkey]);
next($arr[$subkey]);
echo current($arr[$subkey]);
answered 2008-12-29T16:59:48.367

Your Answer