Page 1 of 1
Associative arrays
Posted: Sat Dec 12, 2009 11:41 am
by code_worm
Hi
I have made up an associative array, what i want to do is display the array key
Here is my array.
So i will display CM2013..
any ideas? I looked at the key function but this must need to be incremented or something as i can only get the first key displayed.
Code: Select all
$module_name['CM2013'] = "Professional Development in Computing";
$module_name['CM3004'] = "Systems Development";
$module_name['CM3006'] = "Internet Based Programming";
$module_name['CM3008'] = "Object Oriented Programming";
$module_name['CM3017'] = "Database Systems";
$module_name['CM3032'] = "Project Management in a Computing Enviroment";
$module_name['CM3056'] = "Interactive Multimedia";
$module_name['CM3ELECTIVE'] = "Elective 1";
$module_name['CM4003'] = "Intranet Systems Development";
$module_name['CM4008'] = "Human Computer Interaction";
$module_name['CM4018'] = "Honours Individual Project (Part 1)";
$module_name['CM4ELECTIVE(1)'] = "Elective 1";
$module_name['CM4002'] = "Information Strategy Planning";
$module_name['CM4018'] = "Honours Individual Project (Part 1)";
$module_name['CM4062'] = "Multimedia Programming";
$module_name['CM4ELECTIVE'] = "Elective 2";
Re: Associative arrays
Posted: Sat Dec 12, 2009 12:01 pm
by Christopher
To display an array element do:
You and also assign to an array like this:
Code: Select all
$module_name = array(
'CM2013' => "Professional Development in Computing",
'CM3004' => "Systems Development",
'CM3006' => "Internet Based Programming",
'CM3008' => "Object Oriented Programming",
'CM3017' => "Database Systems",
'CM3032' => "Project Management in a Computing Enviroment",
'CM3056' => "Interactive Multimedia",
'CM3ELECTIVE' => "Elective 1",
'CM4003' => "Intranet Systems Development",
'CM4008' => "Human Computer Interaction",
'CM4018' => "Honours Individual Project (Part 1)",
'CM4ELECTIVE(1)' => "Elective 1",
'CM4002' => "Information Strategy Planning",
'CM4018' => "Honours Individual Project (Part 1)",
'CM4062' => "Multimedia Programming",
'CM4ELECTIVE' => "Elective 2",
);
Re: Associative arrays
Posted: Sat Dec 12, 2009 12:17 pm
by code_worm
Ok, i know how to display the array elements
But what i want to know is how to actually display the key, such as 'CM2013'
Whats wrong with the way i have declared my arrays?
It states in w3schools site this is the correct way?...
Re: Associative arrays
Posted: Sat Dec 12, 2009 1:12 pm
by AbraCadaver
Code: Select all
foreach($module_name as $key => $value) {
echo $key;
}
// or
foreach(array_keys($module_name) as $key) {
echo $key;
}
Re: Associative arrays
Posted: Sat Dec 12, 2009 1:52 pm
by code_worm
What about if i want to access them singularly?
i have to populate my table with the keys, 1 key per table cell
Re: Associative arrays
Posted: Sat Dec 12, 2009 2:24 pm
by AbraCadaver
code_worm wrote:What about if i want to access them singularly?
i have to populate my table with the keys, 1 key per table cell
I have no idea what you are asking. Maybe a little more explanation.
Re: Associative arrays
Posted: Sat Dec 12, 2009 2:28 pm
by code_worm
Ok sorry, In your code you assign all the keys to a variable.
Well what i would really want to do is assign the keys to an array so i can access each key individually.
Im trying to break up your code to try implement it in array form as we speak
Re: Associative arrays
Posted: Sat Dec 12, 2009 2:41 pm
by code_worm
Not really got far with this if you could help me out with code that assigns the values to an array rather than the $key variable i would be very gratefull
Re: Associative arrays
Posted: Sat Dec 12, 2009 2:47 pm
by AbraCadaver
code_worm wrote:Ok sorry, In your code you assign all the keys to a variable.
Well what i would really want to do is assign the keys to an array so i can access each key individually.
Im trying to break up your code to try implement it in array form as we speak
Or if you want to put them in a comma separated string (may come in handy some day):
Code: Select all
$key_list = implode(',', array_keys($module_name));
Re: Associative arrays
Posted: Sat Dec 12, 2009 2:54 pm
by code_worm
Thank you very much man, Your a star!
Re: Associative arrays
Posted: Sat Dec 12, 2009 3:33 pm
by code_worm
The code did work up untill the last 2 keys...
any suggestions?
Heres the array...
Code: Select all
$module_name['CM2013'] = "Professional Development in Computing";
$module_name['CM3004'] = "Systems Development";
$module_name['CM3006'] = "Internet Based Programming";
$module_name['CM3008'] = "Object Oriented Programming";
$module_name['CM3017'] = "Database Systems";
$module_name['CM3032'] = "Project Management in a Computing Enviroment";
$module_name['CM3056'] = "Interactive Multimedia";
$module_name['CM3ELECTIVE'] = "Elective 1";
$module_name['CM4003'] = "Intranet Systems Development";
$module_name['CM4008'] = "Human Computer Interaction";
$module_name['CM4018'] = "Honours Individual Project (Part 1)";
$module_name['CM4ELECTIVE'] = "Elective 1";
$module_name['CM4002'] = "Information Strategy Planning";
$module_name['CM4018'] = "Honours Individual Project (Part 2)";
$module_name['CM4062'] = "Multimedia Programming";
$module_name['CM4ELECTIVE'] = "Elective 2";
Every key works great accept the last 2, ive been thinking what it could be but it totally puzzles me
Re: Associative arrays
Posted: Sat Dec 12, 2009 5:05 pm
by AbraCadaver
I don't know what you mean by didn't work. Do:
then
Code: Select all
$keys = array_keys($module_name);
print_r($keys);
Re: Associative arrays
Posted: Sat Dec 12, 2009 5:50 pm
by code_worm
Thanks for that print code
I realised that my problem was because that some keys were identicle which caused a problem but i have now rectified it. Thank you you have saved me time, stress, among other things haha