A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearAccess PHP array element with a function? - page 1
User InfoPosts
Access PHP array element with a function?#1
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:


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')



Any suggestions?

posted date: 2008-12-28 10:54:00

Re: Access PHP array element with a function?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-28 10:54:01

Re: Access PHP array element with a function?#3
I very much doubt there is such a function, but it's trivial to writefunction getvalue($array, $key){ return $array[$key];}Edit: As of PHP 5.4, you can index array elements directly from function expressions, current($arr)['item'].

posted date: 2008-12-28 11:05:00

Re: Access PHP array element with a function?#4
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.

posted date: 2008-12-28 11:13:00

Re: Access PHP array element with a function?#5
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.

posted date: 2008-12-28 11:19:00

Re: Access PHP array element with a function?#6
You have already found the best solution: $arr[key($arr)]['item']

posted date: 2008-12-28 12:27:00

Re: Access PHP array element with a function?#7
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]);

posted date: 2008-12-29 08:59:00

Re: Access PHP array element with a function?#8
I often useforeach ($arr as $key=>$val) { $val['item'] /*$val is the value of the array*/ $key /*$key is the key used */}instead ofnext($arr)/current($arr)

posted date: 2008-12-29 09:13:00

Re: Access PHP array element with a function?#9
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.

posted date: 2008-12-29 09:14:00

Re: Access PHP array element with a function?#10
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. :)

posted date: 2008-12-29 09:17:00

Re: Access PHP array element with a function?#11
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'])?

posted date: 2008-12-29 11:29:00

Re: Access PHP array element with a function?#12
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');

posted date: 2009-04-28 20:32:00

select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.