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')
)
);
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
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
--
Thanks guys! It works! .....also thanks for the tip about the missing
quotes...hehehe