Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do i get the index key NOT the key string of an array?

0 views
Skip to first unread message

SM

unread,
Oct 9, 2008, 11:52:25 PM10/9/08
to
Hello,
I have an array that looks like the following one (see below). I need
to get the key index or position given a certain value.
So if the value is 22222222 the index/position in the array $cds[1] is
8. If XXXXXXXX the position is 4.

So my question is: how do i get the key index or position?
Thanks
Marco

$cds = array(
1=> array(
ClOHCQiD=> array('xxx', 'yyy'),
pZrhrRRX=> array('xxx', 'yyy'),
Lk3AGW6P=> array('xxx', 'yyy'),
XXXXXXXX=> array('xxx', 'yyy'),
t1knTITi=> array('xxx', 'yyy'),
yJd6OBlM=> array('xxx', 'yyy'),
J1tiIzSE=> array('xxx', 'yyy'),
22222222=> array('xxx', 'yyy')
)
);

Curtis

unread,
Oct 10, 2008, 3:41:59 AM10/10/08
to

I can't seem to think of anything too elegant right now, but you could
use a loop to create a new array with indexing starting from 1:

<?php


$cds = array(
1=> array(

'ClOHCQiD'=> array('xxx', 'yyy'),
'pZrhrRRX'=> array('xxx', 'yyy'),
'Lk3AGW6P'=> array('xxx', 'yyy'),
'XXXXXXXX'=> array('xxx', 'yyy'),
't1knTITi'=> array('xxx', 'yyy'),
'yJd6OBlM'=> array('xxx', 'yyy'),
'J1tiIzSE'=> array('xxx', 'yyy'),
'22222222'=> array('xxx', 'yyy')
)
);
$keys = array_keys($cds[1]);
$i = 0;
while ($i < sizeof($keys))
$keypos[$i+1] = $keys[$i++];

print_r($keypos);
?>

Just a note about the keys in your $cds hash, if you don't put quotes
around the keys (XXXXXXXX, t1knTITi, etc.), PHP will complain with
notices, since it thinks you're trying to use undefined constants.

--
Curtis

"Álvaro G. Vicario"

unread,
Oct 10, 2008, 3:44:39 AM10/10/08
to
SM escribió:

Associative arrays do not have numeric keys so there's nothing you can
actually "get". You can, however, do many tricks. For instance:

index= array_search('XXXXXXXX', array_keys($cds[1]));
if($index!==FALSE){
$position = $index+1;
}

If your array keys are meaningless, why don't you simply use a numeric
array?

cds = array(
1=> array(
0 => array('ClOHCQiD', 'xxx', 'yyy'),
...
);

Or, even better, why don't you encapsulate all this logic in classes?

class Library{
public function addCD(CD $new_cd){
}
}

class CD{
public function addTrack(Track $new_track){
}
public function findTrack($track_id){
}
}

class Track{
}

Etc.

BTW, I can't believe you've defined constants for each key (apparently
random) key, so you're missing quotes around they keys and
error_reporting=E_ALL.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

SM

unread,
Oct 10, 2008, 4:20:03 AM10/10/08
to
On Oct 10, 3:44 am, "Álvaro G. Vicario"
> --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain

> -- Mi sitio sobre programación web:http://bits.demogracia.com
> -- Mi web de humor al baño María:http://www.demogracia.com
> --

Thanks guys! It works! .....also thanks for the tip about the missing
quotes...hehehe

0 new messages