Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get key name in multidimensional array

Status
Not open for further replies.

dcnguyen

Technical User
Mar 22, 2005
54
0
0
US
I have an array like this:

Code:
Array
(
    [School Name Here ] => Array
        (
            [status] => private
            [Girl's Basketball] => Array
                (
                    [wins] => 16
                    [losses] => 10
                )

            [Baseball] => Array
                (
                    [wins] => 11
                    [losses] => 4
                )


        )
...more entries

}

What I want to do is iterate through the array and get the school name of each entry in this main array...but when I do this:

Code:
foreach($arr as $school=>$value){
	print_r(key($value));

}


I get 'status', which is the keyname of a property of the school...Am I using foreach wrong here?
 
If you want the school name, you do, indeed, want the key. But I don't know what you're trying to do with your invocation of key(). You have the key already from the line that reads

foreach($arr as $school=>$value){


This script:
Code:
<?php

$a = array
(
	'School Name Here' => Array
	(
		'status' => 'private',
		'Girl\'s Basketball' => Array
		(
			'wins' => 16,
			'losses' => 10
		),
		'Baseball' => Array
		(
			'wins' => 11,
			'losses' => 4
		)
	)
);

foreach ($a as $key => $school_data)
{
	print $key;
}
?>

Outputs:

[tt]School Name Here[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks. As I thought...I didn't understand foreach completely...I thought $key (as you used it) was also an array. Doh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top