-3

I have string is:

$string = '200AUD, 150USD, 80GBP, 1250000VND';

How Can I convert from $string to an array with key => value.

Key: AUD => Value 200
Key: USD => Value 150
Key: GBP => 80
Key: VND => 1250000

Updated:

@Mark Baker my php version is 5.4 so I cannot test your code, thanks

@Aleatoric I'm use this code, but the array return some space character on key and value:

                Array
(
    [ CHF] => 130 
    [  GBP] =>  80 
    [  USD] =>  125 
    [  DKK] =>  750 
    [  PLN] =>  400 
    [  CZK] =>  2500 
    [  YTL] =>  175 
    [  BGN] =>  175 
    [  RUB] =>  4000 
    [  RON] =>  400 
    [  SEK] =>  1000 
)

I have use trim() to strips all space, but can you tell me best idea for this issue without use trim() function?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Diep Tang
  • 37
  • 2
  • 11

3 Answers3

3

You're going to have to explode the string first with:

$array1 = explode(',', $string);

and then iterate over the elements with a foreach loop to split the strings and put them into an associative array:

$array2 = array();
foreach($array1 as $string){
    $currency = trim(preg_replace("/[0-9]+/", "", $string));
    $value = trim(preg_replace("/[a-zA-Z]+/", "", $string));
    $array2[$currency] = $value;
}
WinterMute
  • 519
  • 3
  • 9
  • Several fixes. First, remove the whitespace in your initial string. So you would have $string = '200AUD,150USD,80GBP,1250000VND'; Alternatively, include the whitespace with the explode(", ", $string); There are other ways to do it, but honestly this is the use-case that trim is for. What's wrong with using trim()? – WinterMute Mar 31 '14 at 15:55
  • Trim is also better in the case that you have input that may or may not contain extra whitespace. The other methods will fail and you will have to apply additional checks to the input to make sure it conforms. Trim is your best bet and I've updated my answer accordingly. – WinterMute Mar 31 '14 at 15:57
1

Requires PHP >= 5.5

$string = '200AUD, 150USD, 80GBP, 1250000VND';

$currencyArray = array_column(
    array_map(
        function ($value) {
            return sscanf($value, '%d%s');
        },
        explode(',', $string)
    ),
    0,
    1
);

var_dump($currencyArray);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

I prefer the use of sscanf() like in @MarkBaker's answer because it allows the explicit casting of the numeric values as integer-type, but to minimize the number of cycles, I'll recommend array_reduce() because it will allow key declarations.

Code: (Demo)

$string = '200AUD, 150USD, 80GBP, 1250000VND';

var_export(
    array_reduce(
        explode(',', $string),
        function ($result, $amount) {
            [
                1 => $currency,
                0 => $result[$currency]
            ] = sscanf($amount, '%d%s');
            return $result;
        },
        []
    )
);

Output:

array (
  'AUD' => 200,
  'USD' => 150,
  'GBP' => 80,
  'VND' => 1250000,
)

While destructuring sscanf()'s return value, the above script declares a variable for the second element ([1]) first. Then that variable can be immediately used to push the amount into the array with the related currency as the key.


Without any tricky destructuring, associative elements can be added to the result array using the union operator +. (Demo)

var_export(
    array_reduce(
        explode(',', $string),
        function ($result, $money) {
            sscanf($money, '%d%s', $amount, $currency);
            return $result + [$currency => $amount];
        },
        []
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136