Associative arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Associative arrays

Post 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";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Associative arrays

Post by Christopher »

To display an array element do:

Code: Select all

echo $module_name['CM2013'];
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",
    );
(#10850)
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post 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?...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Associative arrays

Post by AbraCadaver »

Code: Select all

foreach($module_name as $key => $value) {
    echo $key;
}
 
// or
 
foreach(array_keys($module_name) as $key) {
    echo $key;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Associative arrays

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post 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
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Associative arrays

Post 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

Code: Select all

$keys = array_keys($module_name);
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));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post by code_worm »

Thank you very much man, Your a star!
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Associative arrays

Post by AbraCadaver »

I don't know what you mean by didn't work. Do:

Code: Select all

print_r($module_name);
then

Code: Select all

$keys = array_keys($module_name);
print_r($keys);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Associative arrays

Post 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
Post Reply