php key max
- Started
- Last post
- 10 Responses
- rabattski
hey there, i need to find the maximum value (integer) of a specific key in a multidimensional array. using max doesn't cut it since it still takes other key values in account as well. so basically i have to ignore all keys accept one. anyone got a clue?
- UndoUndo0
cannot you not return all keys and sort them with DESC and pick the top one? or bottom one?
- rabattski0
hmmm.... that's an interesting workaround. lemme try that. cheers!
- rabattski0
didn't really work, still problems with integers versus strings. did find a solution though and that's just by looping thru the array for the specific key.
- deplifer0
How does your multidimensional array look?
What are you looking for?
if your key has an name as
$array = array(key1 => 100,
key2 =>200,..);you can simply find the value of key2 by indexing the name ::
$value = $array['key2'];or to operate on the array use
foreach($array as $value){
// operate on $value
}foreach($array as $key => $value){
// do stuff
}
- rabattski0
cheers, it's quite a complex array, but i solved it already, basically did what you said in the last bit. multidim. arrays always find a way to fuck up my brain :)
- deplifer0
Good it solved out for you.
Know exactly what you mean.
- rabattski0
dude, i was dreaming in arrays last couple of nights, no kidding, and that must not be healthy.
- deplifer0
Hahaha that's to much.
How come, you got stuck in a
loop??Really must not be healthy had some dreams of that type to when i have got to stuck with some code for a couple of hours.
- davetufts0
// $your_array = Your Array
$keys = array_keys($your_array);
$max_key = max($keys);// or with less code:
echo max(array_keys($your_array));
- rabattski0
dave, max doesn't work, it's quite a complex multidimensional array and it doesn't give me the output i want (logically) but i've already figured it out. thanks though.
deplifer, it's just that i've been coding for a couple of days straight and all regarding dynamically generated multidimensional associative arrays. it's like tetris, play it too long and you'll tetris in your sleep. in my sleep i was putting dreams into arrays like if is_array($dream) etc.