We help IT Professionals succeed at work.

array_search not working, looking for resolutions.

dllfile
dllfile asked
on
1,325 Views
Last Modified: 2008-04-24
I have the following code (see below) Everything works except for the array_search. It always seems to echo empty.
/*----------------------------------*/
define('BASE_DIR', dirname(__FILE__));

$langencoding = array(
      'ISO-8859-1' => 'de',
      'ISO-8859-1' => 'en-us',
      'ISO-8859-1' => 'en',
      'ISO-8859-1' => 'fr',
      'ISO-8859-1' => 'it',
      'ISO-8859-1' => 'es'
      );

function languages() {
      $browserlang = array(
            'de' => 'german',
            'en-us' => 'english',
            'en' => 'english',
            'fr' => 'french',
            'it' => 'italian',
            'es' => 'spanish'
      );
      return $browserlang;
}

$browserlang = languages();
$index = '';
$complete = '';
$user_languages = array();

if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
      $languages = strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
      $languages = explode(",", $languages);
      foreach ($languages as $language_list) {
            $temp_array = array();
            $temp_array[0] = substr($language_list, 0, strcspn($language_list, ';'));
            $temp_array[1] = substr($language_list, 0, 2);
            $user_languages[] = $temp_array;
      }
      for ($i = 0; $i < count($user_languages); $i++) {
            foreach ($browserlang as $index => $complete) {
                  if ($index == $user_languages[$i][0]) {
                        $user_languages[$i][2] = $complete;
                        $user_languages[$i][3] = substr($complete, 0, strcspn($complete, ' ('));
                  }
            }
      }
      $tmplangfile = $user_languages[0][2];
      if (file_exists(BASE_DIR.'/incl/lang/lang-'.$tmplangfile.'.php')) {
            $Language = $user_languages[0][1];
            $Lang_File = $tmplangfile;
            $lowercaselng = strtolower($Language);
            $CharSet = array_search($lowercaselng, $langencoding); // Why does $Charset = array search not work here??
            echo "STEP 1<br /><br />\n";
      } else {
            $Language = 'en';
            $Lang_File = 'english';
            $CharSet = 'ISO-8859-1';
      }
} else {
      $Language = 'en';
      $Lang_File = 'english';
      $CharSet = 'ISO-8859-1';
}
include_once(BASE_DIR.'/incl/lang/lang-'.$Lang_File.'.php');

echo "1: $Language<br />\n";
echo "2: $Lang_File<br />\n";
echo "3: $CharSet<br />\n";
/*----------------------------------*/
I had it working before I had put in language file exists checks, but now I am stumped as to why $CharSet always returns empty. my $Language echos out "en" and $Lang_File echos out "english" but when I do the array search for $Language in langencoding array it returns empty. Any help would be greatly appriciated, it has to be something simple I have just been overlooking. I had made a quick test array using fruit names (Apples, Oranges) and put some random text line for each "Apples are yummy" "Oranges are Orange" and did an array search just as above in the same location and it worked fine, so I am stumped as to why mine is not working for $CharSet.
Comment
Watch Question

Commented:
ok first of all,
do you know what the following line will return?
  $languages = strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]);

answer:
en-us,en;q=0.5

reply me if you still don't get it

Author

Commented:
yeah, but I am not searching for that string, I get the final language and have put into a variable that is set to "en" for me $Language = en

Then I am searching array $langencoding for en which should return ISO-8859-1

// $Language = $user_languages[0][1]; // returns 'en'
// $lowercaselng = strtolower($Language); // just incase it is in caps
$CharSet = array_search($lowercaselng, $langencoding);  // search en in array $langencoding

$langencoding = array( // array $langencoding
      'ISO-8859-1' => 'de',
      'ISO-8859-1' => 'en-us',
      'ISO-8859-1' => 'en',
      'ISO-8859-1' => 'fr',
      'ISO-8859-1' => 'it',
      'ISO-8859-1' => 'es'
      );

Author

Commented:
hmm, I just tried this code to see

$language_list = strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
echo "LangList: $language_list";

and for me it just echos out: en-us

Author

Commented:
Figured out the problem after testing some new code. However, I am unsure how to fix it.

Seems the array for getting the Charset the "last" one in the array is the only one that will return any data.. Not sure why. You can easily test this with this code.

$langfile = array(
      'German' => 'de',
      'English' => 'en',
      'English' => 'en-us',
      'French' => 'fr',
      'Italian' => 'it',
      'Spanish' => 'es'
);

$langcharset = array(
      'ISO-8859-1' => 'de',
      'ISO-8859-1' => 'en',
      'ISO-8859-1' => 'en-us',
      'ISO-8859-1' => 'fr',
      'ISO-8859-1' => 'it',
      'ISO-8859-1' => 'es'
);

$default_language = strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$testfulllang = array_search($default_language, $langfile);
$tmplangfile = strtolower($testfulllang);
$tmpencoding = array_search($default_language, $langcharset);
echo "<font color=\"#ffffff\">tmplangfile: $tmplangfile<br />Default Language: $default_language<br />Full Language: $testfulllang<br />Encoding: $tmpencoding</font><br />";

If you replace
$tmpencoding = array_search($default_language, $langcharset);

with
$tmpencoding = array_search('fr', $langcharset);

You will see it returns the ISO-8895-1, but if you replace 'fr' with any other in the list, it always returns nothing. This a bug in PHP or am I missing something here ???

Author

Commented:
Maybe if I move the charset portion to SQL it would be better? Or anyone have a way to get it working as a hard coded array? Is there a way to search an array based on the first value instead of the second? I am no "expert" on php, I know enough to do some small things, but I am getting confused here as to why it wont work.

Was thinking an SQL table that lists each language/full name/short/charset/ect.. then I could just grab the correct one from SQL based on the browsers default accept. ? Would that work well or would it be better to keep it in a file?
CERTIFIED EXPERT

Commented:
>> Seems the array for getting the Charset the "last" one in the array is the only one that will return any data.

The reason (taken from http://us2.php.net/manual/en/function.array.php):

"Note that when two identical index are defined, the last overwrite the first."
CERTIFIED EXPERT

Commented:
Have you tried reversing the key/value pairs?

$langcharset = array(
      'de'      => 'ISO-8859-1',
      'en'      => 'ISO-8859-1',
      'en-us' => 'ISO-8859-1',
      'fr'        => 'ISO-8859-1',
      'it'        => 'ISO-8859-1',
      'es'      => 'ISO-8859-1'
);

Author

Commented:
Thnx for the reply FishMonger, yes, I had tried that, but I was under the impression that array_search searches the second part in series and not the first? Or is there another way I need to search it?
Example:

$langcharset2 = array(
      'de'      => 'ISO-8859-1',
      'en'      => 'ISO-8859-1',
      'en-us' => 'ISO-8859-1',
      'fr'        => 'ISO-8859-1',
      'it'        => 'ISO-8859-1',
      'es'      => 'ISO-8859-1'
);

$lang = 'en';
$testreverse = array_search($lang, $langcharset2);
echo "Reversed: $testreverse<br />\n";  // Still returns nothing

Not sure how to search the left side values and return the corresponding one on the right. As I said, im new to this stuff, so not something I completly understand atm.
CERTIFIED EXPERT

Commented:
>> I was under the impression that array_search searches the second part in series and not the first

Yes, array_search matches the second part (the value of the associative array) and returns the first part (the key, which must be unique).

Your code is a little convoluted, so I'm not exactly sure what you want to accomplish.  All of the character sets in your code are 'ISO-8859-1'.  If you're not going to have any other character sets, it doesn't make sense to use an associative array.  But if that's what you want, it would be easier and more efficient to call the value directly instead of using array_search.

$lang = 'en';
echo "Reversed: $langcharset2[$lang]<br />\n";   //  returns 'ISO-8859-1'

If that's not what you want, please give more details on what you want to accomplish.  Keep in mind that I rarely work with PHP, I much prefer Perl, which is where your question was cross posted.

Author

Commented:
yes, I will have more character sets, but befoer I go adding 100 languages to the array, I need to get it working first. And I need to be able to return the charset for the apprpriate language. (such as Arabic windows-1256) but for now, I am just including some of the "major" languages which happen to all use the same charset. But getting the charset returned so I can use it is what I need.

Once I detect the language, I need to be able to search for the language such as "en" and return the charset for it.

Author

Commented:
sorry to double post again, but so you can see (and I have not went through finding all the correct charsets yet, some are missing the comments as I have to find/verify each language charset before adding them. So, the list currently. (unfinished no charset //comments)

      'af' => 'afrikaans',            // ISO-8859-1
      'sq' => 'albanian',            // ISO-8859-1
      'ar' => 'arabic',            // 1256
      'ar-dz' => 'arabic',            // algeria
      'ar-bh' => 'arabic',            // bahrain
      'ar-eg' => 'arabic',            // egypt
      'ar-iq' => 'arabic',            // iraq
      'ar-jo' => 'arabic',            // jordan
      'ar-kw' => 'arabic',            // kuwait
      'ar-lb' => 'arabic',            // lebanon
      'ar-ly' => 'arabic',            // libya
      'ar-ma' => 'arabic',            // morocco
      'ar-om' => 'arabic',            // oman
      'ar-qa' => 'arabic',            // qatar
      'ar-sa' => 'arabic',            // Saudi Arabia
      'ar-sy' => 'arabic',            // syria
      'ar-tn' => 'arabic',            // tunisia
      'ar-ae' => 'arabic',            // U.A.E
      'ar-ye' => 'arabic',            // yemen
      'hy' => 'armenian',
      'ast' => 'asturian',
      'eu' => 'basque',
      'be' => 'belarusian',
      'bs' => 'bosanski',            // bosnian -bosanski
      'bg' => 'bulgarian',
      'ca' => 'catalan',
      'zh' => 'chinese',
      'zh-cn' => 'chinese',            // China
      'zh-hk' => 'chinese',            // Hong Kong
      'zh-sg' => 'chinese',            // Singapore
      'zh-tw' => 'chinese',            // Taiwan
      'hr' => 'croatian',            // 1250
      'cs' => 'czech',
      'ISO-8859-1' => 'da',
      'dcc' => 'desi',            // Deccan, India
      'nl' => 'dutch',            // ISO-8859-1
      'nl-be' => 'dutch',            // Belgium
      'ISO-8859-1' => 'en',
      'ISO-8859-1' => 'en-au',            // Australia
      'ISO-8859-1' => 'en-bz',            // Belize
      'ISO-8859-1' => 'en-ca',            // Canada
      'ISO-8859-1' => 'en-ie',            // Ireland
      'ISO-8859-1' => 'en-jm',            // Jamaica
      'ISO-8859-1' => 'en-nz',            // New Zealand
      'ISO-8859-1' => 'en-ph',            // Philippines
      'ISO-8859-1' => 'en-za',            // South Africa
      'ISO-8859-1' => 'en-tt',            // Trinidad
      'ISO-8859-1' => 'en-gb',            // United Kingdom
      'ISO-8859-1' => 'en-us',            // United States
      'ISO-8859-1' => 'en-zw',            // Zimbabwe
      'eo' => 'esperanto',
      'et' => 'estonian',
      'eu' => 'euraska',            // ISO-8859-1
      'fo' => 'faeroese',
      'fi' => 'finnish',            // ISO-8859-1
      'fr' => 'french',            // ISO-8859-1
      'fr-be' => 'french',            // Belgium
      'fr-ca' => 'french',            // Canada
      'fr-fr' => 'french',            // France
      'fr-lu' => 'french',            // Luxembourg
      'fr-mc' => 'french',            // Monaco
      'fr-ch' => 'french',            // Switzerland
      'gl' => 'galego',            // ISO-8859-1
      'ka' => 'georgian',
      'de' => 'german',            // ISO-8859-1
      'de-at' => 'german',            // Austria
      'de-de' => 'german',            // Germany
      'de-li' => 'german',            // Liechtenstein
      'de-lu' => 'german',            // Luxembourg
      'de-ch' => 'german',            // Switzerland
      'el' => 'greek',            // ISO-8859-7
      'he' => 'hebrew',
      'hu' => 'hungarian',            // ISO-8859-2
      'is' => 'icelandic',            // ISO-8859-1
      'id' => 'indonesian',            // ISO-8859-1
      'ga' => 'irish',
      'it' => 'italian',                  // ISO-8859-1
      'it-ch' => 'italian',            // Switzerland
      'ja' => 'japanese',
      'ko' => 'korean',
      'ko-kp' => 'korean',            // North Korea
      'ko-kr' => 'korean',            // South Korea
      'ku' => 'kurdish',            // 1254
      'lv' => 'latvian',
      'lt' => 'lithuanian',            // 1257
      'mk' => 'macedonian',            // 1251
      'ms' => 'malayu',
      'no' => 'norwegian',            // ISO-8859-1
      'nb' => 'norwegian',            // bokmal
      'nn' => 'norwegian',            // nynorsk
      'pl' => 'polish',            // ISO-8859-2
      'pt' => 'portuguese',            // 28591, Latin-I, iso-8859-1
      'pt-br' => 'brazilian',            // Brazil
      'ro' => 'romanian',            // 28592, Central Europe, iso-8859-2
      'ru' => 'russian',            // 1251 ANSI
      'gd' => 'scots gealic',
      'sr' => 'serbian',
      'sk' => 'slovak',            // 1250 ANSI
      'sl' => 'slovenian',            // 28592, Central Europe, iso-8859-2
      'es' => 'spanish',            // 28591, Latin-I, iso-8859-1
      'es-ar' => 'spanish',            // Argentina
      'es-bo' => 'spanish',            // Bolivia
      'es-cl' => 'spanish',            // Chile
      'es-co' => 'spanish',            // Colombia
      'es-cr' => 'spanish',            // Costa Rica
      'es-do' => 'spanish',            // Dominican Republic
      'es-ec' => 'spanish',            // Ecuador
      'es-sv' => 'spanish',            // El Salvador
      'es-gt' => 'spanish',            // Guatemala
      'es-hn' => 'spanish',            // Honduras
      'es-mx' => 'spanish',            // Mexico
      'es-ni' => 'spanish',            // Nicaragua
      'es-pa' => 'spanish',            // Panama
      'es-py' => 'spanish',            // Paraguay
      'es-pe' => 'spanish',            // Peru
      'es-pr' => 'spanish',            // Puerto Rico
      'es-es' => 'castellano',      // Spain
      'es-uy' => 'spanish',            // Uruguay
      'es-ve' => 'spanish',            // Venezuela
      'sv' => 'swedish',
      'sv-fi' => 'swedish',            // Finland
      'sw' => 'swahili',            // Kenya and Tanzania
      'th' => 'thai',                  // 874
      'tr' => 'turkish',            // 1254
      'ug' => 'uighur',            // ISO-8859-1, 28591 Turkish, Uzbek, China
      'uk' => 'ukrainian',
      'vi' => 'vietnamese',
      'cy' => 'welsh',
      'xh' => 'xhosa',
      'yi' => 'yiddish',
      'zu' => 'zulu'
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thnx, that was the way I had it the first time, but when searching for ISO-8859-1 would it not return more than 1? Not that it would matter much since the charset would be the same in that case.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.