Re: foreach on a 3d array

From: Date: Wed, 25 Oct 2006 07:28:56 +0000
Subject: Re: foreach on a 3d array
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-243599@lists.php.net to get a copy of this message
On Tue, 24 Oct 2006 23:55:49 +0200, M.Sokolewicz wrote: > Dotan Cohen wrote: >> On 24/10/06, Chris Boget <chris.boget@wild.net> wrote: >>> > $languages = array( >>> > "af" => array("Afrikaans", "Afrikaans", >>> > "South Africa"), >>> > "sq" => array("Albanian", "Shqipe", >>> > "Albania") ); >>> > >>> > foreach ($languages as $language){ >>> > if ( strstr( $_HTTP_ACCEPT_LANGUAGE, $language) ) { >>> > print"<center>You are from >>> > ".$language[2]."!</center>"; >>> > } >>> > } >>> >>> What you want is something like this: >>> >>> foreach ($languages as $language => $valueArray ){ >>> if ( strstr( $_HTTP_ACCEPT_LANGUAGE, $language) ) { >>> print"<center>You are from >>> ".$valueArray[2]."!</center>"; >>> } >>> } >>> >>> Your example is setting the variable $language to the array for each >>> iteration. >> >> Thanks, I see what I was missing. >> >>> So the first iteration, >>> >>> $language is set to array("Afrikaans", "Afrikaans", "South >>> Africa") >>> >>> and the second iteration, >>> >>> $language is set to array("Albanian", "Shqipe", >>> "Albania") >> >> That much I knew. Thanks, Chris. >> >> Dotan Cohen >> >> http://essentialinux.com/ >> http://technology-sleuth.com/ > > Why not just do > > if(isset($language[$_HTTP_ACCEPT_LANGUAGE])) { > print '<center>You are from > '.$language[$_HTTP_ACCEPT_LANGUAGE][3].'!</center>'; > } else { > print '<center>where are you from?!</center>'; > } Because that wouldn't work :) This variable may contain stuff like "nl,en-us;q=0.7,en;q=0.3". You'll need to do something with this variable first to use array_key_exists (or as you do, isset). That said, I agree that strstr() might not be the best solution. An other note, Dotan: shouldn't $_HTTP_ACCEPT_LANGUAGE actually be $_SERVER['HTTP_ACCEPT_LANGUAGE']? Ivo

« previous php.general (#243599) next »