User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » php associative array - get key index? Page [1]  
agentlion
All American
13936 Posts
user info
edit post

i hate asking simple programming questions here, but i just can't find how to do this anywhere.

I have an associate array with strings as Keys and Values, but would also like to know the numerical index of a particular (unique) Key. I have read in key:value pairs from an INI file, but the number of the key:value is also important. I don't want to do double arrays or something like that - this should be extremely simple. Is this just not an option with associative arrays? If an array is associative, does it lose all its capability to count the indicii numerically? All the current(), value(), key(), reset(), array_key_exists() functions don't exactly what i need. Do I have to do something stupid like foreach the array and keep a counter and break when i find a match?
http://de2.php.net/manual/en/ref.array.php

you would think there would be a function like in_array() that returned an index, instead of just a boolean. i don't see any index_of(key) functions or anything.

[Edited on February 17, 2006 at 6:29 PM. Reason : .]

2/17/2006 6:27:21 PM

mattc
All American
1172 Posts
user info
edit post

this is kinda ghetto but...


$mykeypos = array_search("keyname", array_keys($myarray));

2/17/2006 10:35:09 PM

Noen
All American
31346 Posts
user info
edit post

no there is no inherent functionality in PHP to do this.

if you assign a string literal as a key, then that IS the key. There is no secondary numerical index hidden from you. This shouldn't be a suprise or anything.

If you want double indexes, make a multidimensional array and write a quick function to overload the standard functionality for what you need.

2/18/2006 1:23:15 AM

agentlion
All American
13936 Posts
user info
edit post

^^ that's what i needed. thanks

2/18/2006 12:20:25 PM

Noen
All American
31346 Posts
user info
edit post

^you do realize that is sloooow as hell right? Just making sure.

2/18/2006 1:37:14 PM

State409c
Suspended
19558 Posts
user info
edit post

Not everyone is making enterprise class apps. Just because your great ideas weren't needed or cared about doesn't mean you need to add additional "but what about me" commentary.

Here, I'll make your next post for you.

It's trivial to do what I suggested and works tons faster. Anyone using the other [albeit extremely simple idea <- my interjection] suggested technique is an idiot and doesn't derserve the privelege of the internet.

2/18/2006 1:54:27 PM

Noen
All American
31346 Posts
user info
edit post

no. I made the comment to make sure he realizes there is a tradeoff.

Since he originally stipulated he didnt want double arrays or anything complicated, one POSSIBLE inference could be drawn that the efficiency of the code is important.

I'm just trying to help, nice troll attempt though

2/18/2006 4:11:17 PM

agentlion
All American
13936 Posts
user info
edit post

yeah, to clarify this is for a female cyclists website who probably gets in the range of 10 views a day or less from people other than herself or manager. I only have FTP access to the site and am barely motivated to do what I'm doing, much less set up a proper database to do all this for me.

But anyway, i decided to go with parallel arrays - it's much simpler, even if parallel arrays are not considered cool or whatnot. In short, i'm storing some key:value pairs in an INI file, and was reading them out into an associative array using parse_ini_file(). I wanted the keys and values, but also the numerical index of each key.

So now, I'm reading into an associative array, then immediately splitting into 2 parallel arrays using array_keys() and array_values(). These are completely static arrays, so there's no danger of going out of sync with each other. I can then use the index to access both arrays to get the appropriate original key:value pair.

So, efficient? no. elegant? probably not. Do I care? not a bit.

2/18/2006 4:36:19 PM

State409c
Suspended
19558 Posts
user info
edit post

Quote :
"that's what i needed. thanks"


The thread should have been over right there. Shut the fuck up Tyler but please post again to win the thread. I'll let you this time.

2/18/2006 5:45:07 PM

 Message Boards » Tech Talk » php associative array - get key index? Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.