简体   繁体   中英

Accessing an array element whos key is an element from another array

How does one do this?

Here's an example; Say I have an array called $Foo.

$Foo = array("cool" => "Manchu", "Mama" => "loofoo", "Lizard" => "Foof on you");

...and then there's this other array...

$OtherArray = array("cool", "Mama", "Lizard");

How can I print $Foo[$OtherArray[1]];

I don't really want to:

$ThisThing = $OtherArray[1];
print "$Foo[$ThisThing]";

You can try (explanation in code comments):

// First value of $OtherArray is cool
// We can access it using $OtherArray[0]. It is also a key in the $Foo array
// This would print the value corresponding to 'cool' key in the $Foo array
echo $Foo[$OtherArray[0]];

// Displays Manchu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM