i-CONICA
i-CONICA

Reputation: 2379

How to parse an ini-formatted string into an associative array?

I'm trying to break up this string,

AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh

Into an array like this

$lang_codes['chinese'] = "zh";

So the name of the language is the key, and the value is the language code. This is really simple, but I just can't get my head around it. I've taken a brake from programming, too long, obviously...

I've tried exploding at \n then using a foreach, exploding again at = but I can't seem to piece it together the way I want.

Upvotes: 1

Views: 332

Answers (5)

mickmackusa
mickmackusa

Reputation: 48070

Your input string is already in a legitimate format call ini (short for initialization). It was historically favorable for declaring program initialization data in a lean, syntax-light format. PHP has a native function designed to parse this data format - parse_ini_format().

There are some nuanced conventions/rules built into ini strings/files (such as sections, comments, reserved words, and special characters to name a few), so if you are not aware of these, please research the topic to avoid potential bugs while creating or parsing ini data.

To cast all keys to lowercase and parse the ini-formatted string, use strtolower() then parse_ini_string(). Demo

$ini = <<<ini
AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh
ini;

var_export(
    parse_ini_string(strtolower($ini))
);

Output:

array (
  'afrikaans' => 'af',
  'albanian' => 'sq',
  'amharic' => 'am',
  'arabic' => 'ar',
  'armenian' => 'hy',
  'azerbaijani' => 'az',
  'basque' => 'eu',
  'belarusian' => 'be',
  'bengali' => 'bn',
  'bihari' => 'bh',
  'bulgarian' => 'bg',
  'burmese' => 'my',
  'catalan' => 'ca',
  'cherokee' => 'chr',
  'chinese' => 'zh',
)

Upvotes: 0

Berry Langerak
Berry Langerak

Reputation: 18859

Although the explode answers are technically correct, I immediately thought that you were trying to parse an INI file. Here's the simpler approach that does exactly what you want.

<?php
$string = "AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh";

$array = array_change_key_case( parse_ini_string( $string ) );

echo $array['chinese'];

Upvotes: 2

newbie
newbie

Reputation: 2452

<?php 
$str ="AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh";

$arr  = explode("\n", $str);
$lang_mapped = array();

foreach ($arr as $line) {
    list ($lang, $code) = explode(" = ", $line, 2);
    $lang_mapped[$lang] = $code;
}
$arr = explode("\n", $str); 
echo "<pre>";
print_r($lang_mapped);
echo "</pre>";
?>

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

Mayb you shold do:

$array = explode("\n" , $string);
$finalArray =array();
$foreach ($array as $value){
      $array2 = explode( ' = ', $value);
      $finalArray[$array2[1]] =  $array2[0];

}

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 238115

I've tried exploding at \n then using a foreach, exploding again at " = "

I think this is exactly the right approach.

$lines = explode("\n", $langs);
$lang_codes = array();

foreach ($lines as $line) {
    list ($lang, $code) = explode(" = ", $line, 2);
    $lang_codes[$lang] = $code;
}

If you want the language to be in lowercase, as in your example ($lang_codes['chinese']), you'll need to call strtolower:

$lang_codes[strtolower($lang)] = $code;

See the PHP manual for more on these functions:

Upvotes: 4

Related Questions