labmixz Posted September 29, 2008 Share Posted September 29, 2008 I need to find the name of the array element, not the value. What's going on is I am pulling data from a db that has to be sorted a certain way... I wanted the array to look like: $array[$key1][$key2][$x][1] Basically $key1 / $key2 are two unique id's for that unit and then that group within that unit.... while $x is just a standard counter... problem is I don't know the names of $key1 / $key2 and I need to know what they are. I'm sure this is a stupid easy question... just can't think of the solution at the moment. Link to comment https://forums.phpfreaks.com/topic/126231-array-question/ Share on other sites More sharing options...
genericnumber1 Posted September 29, 2008 Share Posted September 29, 2008 you can use.. http://us2.php.net/array_keys http://us2.php.net/array_search and a few others, depending upon what you're trying to do... if you're using them for a loop just use foreach($array as $key => $value) {} Link to comment https://forums.phpfreaks.com/topic/126231-array-question/#findComment-652785 Share on other sites More sharing options...
labmixz Posted September 29, 2008 Author Share Posted September 29, 2008 Maybe I didn't explain what I wanted... I already have a foreach looping through everything, that works fine... Here's the array: $array[$key1][$key2][$x][0] $key1 = random from DB $key2 = another random from DB $x = counter getting the info out with a foreach works just fine but I need to know what the name of $key1 is (NOT THE VALUE, but the name of the ARRAY KEY) before I start my loop to get the values... Granted array_keys would work, but I only want to extract that one array key name at that point, not the whole list... and I don't know what the key name COULD be so I can't search for it... because the name of the key is what I need to know to begin with... So basically I know nothing about the array other than how it's setup... I don't know what the first two key are named, just that they are named, I want to know what $array[$key1] is named (not the value, but what the key name is).... Hope that better explains things. Link to comment https://forums.phpfreaks.com/topic/126231-array-question/#findComment-652820 Share on other sites More sharing options...
labmixz Posted September 29, 2008 Author Share Posted September 29, 2008 Actually a better way to say this is, I want to know the name of the key I'm currently in, within my loop, since I don't know the name... when I need to output the name. Link to comment https://forums.phpfreaks.com/topic/126231-array-question/#findComment-652828 Share on other sites More sharing options...
labmixz Posted September 29, 2008 Author Share Posted September 29, 2008 I finally got the information I wanted, I had to create a loop to gather the information and put it into a seperate array when I could easily pull out the names of they elements... $x = 0; foreach($key1 as $key2){ $y = 0; while (list($key, $val) = each($key1)) { $list[$y] = $key; $y++; } echo $list[$x]; $x++; } which lists the name of the element as I'm going through it, which the above is a fairly small portion to what I have and the reasons I needed it are not visable by the above, but I am just glad it's now working. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/126231-array-question/#findComment-652836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.