tags:

views:

2114

answers:

7

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?

+4  A: 

I very much doubt there is such a function, but it's trivial to write

function getvalue($array, $key)
{
  return $array[$key];
}
Adam Wright
I think this is probably what I'll end up doing. If you are correct that there is no such function, it's a pretty bad oversight.
Kyle Cronin
+1  A: 

Have you tried using one of the iterator classes yet? There might be something in there that does exactly what you want. If not, you can likely get what you want by extending the ArrayObject class.

Ant P.
Just looked at those - it looks like that's just an object oriented way to iterate through the array. The ArrayIterator::current returns the current item, much in the same way the current() function does, which isn't really the problem I was having.
Kyle Cronin
A: 

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]);
chelmertz
The iteration I'm performing is really only along the primary array. I said "multidimensional" because the corresponding elements of the main array are themselves arrays, but I don't want to iterate over them - I just want to conveniently access them.
Kyle Cronin
How about treating $arr[$subkey] as an array, or $new_arr = array_values($arr[$subkey])?Footnote: what differs between your requested current($arr)['item'] and current($arr['item'])?
chelmertz
A: 

I often use

foreach ($arr as $key=>$val) {
   $val['item'] /*$val is the value of the array*/
   $key         /*$key is the key used */
}

instead of

next($arr)/current($arr)

Gerrit
I use that too - for the sake of the question I eliminated the fact that I'm actually going down two sorted arrays looking for differences, and I' m pretty sure foreach doesn't support that kind of access, but I could be wrong. Anyways, thanks for your reply. :)
Kyle Cronin
A: 

Without knowing more about your actual data structure I can say that I have twice tried to make a 4D array. Both times I then realised that there was a far better way to structure the data. That way was to use classes.

As all the other answers have posted up possible solutions to your problem I am unable to do that but I do suggest looking at classes because they might be able to help you here.

Teifion
A: 

Thanks for posting this. I literally wasted 30 minutes searching the documentation for this. How could they miss this???

I've got an object inside a forech loop and want to do:

//*** wont work ***
foreach($items as $item) {
    echo do_Stuff_And_Return_Array($item)[2];
}
//*** doesn't exist? ***
foreach($items as $item) {
    echo do_Stuff_And_Return_Array($item).get(2);
}

But I can't do this...

@Adam- thanks for the simple and obvious function.

Jason
A: 

This function might be a bit lenghty but I use it all the time, specially in scenarious like:

if (array_key_exists('user', $_SESSION) === true)
{
    if (array_key_exists('level', $_SESSION['user']) === true)
    {
     $value = $_SESSION['user']['level'];
    }

    else
    {
     $value = 'DEFAULT VALUE IF NOT EXISTS';
    }
}

else
{
    $value = 'DEFAULT VALUE IF NOT EXISTS';
}

Turns to this:

Value($_SESSION, array('user', 'level'), 'DEFAULT VALUE IF NOT EXISTS');

Here is the function:

function Value($array, $key = 0, $default = false)
{
    if (is_array($array) === true)
    {
     if (is_array($key) === true)
     {
      foreach ($key as $value)
      {
       if (array_key_exists($value, $array) === true)
       {
        $array = $array[$value];
       }

       else
       {
        return $default;
       }
      }

      return $array;
     }

     else if (array_key_exists($key, $array) === true)
     {
      return $array[$key];
     }
    }

    return $default;
}

PS: You can also use unidimensional arrays, like this:

Value($_SERVER, 'REQUEST_METHOD', 'DEFAULT VALUE IF NOT EXISTS');
Alix Axel