Comma-separated string to associative array with lowercase keys

5.5k Views Asked by At

I have a string like this:

$string = 'Apple, Orange, Lemone';

I want to make this string to:

$array = array('apple'=>'Apple', 'orang'=>'Orange', 'lemone'=>'Lemone');

How to achieve this?

I used the explode function like this:

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

But that only gives me this:

Array ( [0] => Apple [1] => Orange [2] => Lemone )

Somebody says this question is a duplicate of this SO question: Split a comma-delimited string into an array?

But that question is not addressing my problem. I have already reached the answer of that question, please read my question. I need to change that result array's key value and case. see:

Array
(
    [0] => 9
    [1] => [email protected]
    [2] => 8
)

to like this

Array
(
    [9] => 9
    [[email protected]] => [email protected]
    [8] => 8
)
9

There are 9 best solutions below

2
On BEST ANSWER

You may try :

$string = 'Apple, Orange, Lemone';
$string = str_replace(' ', '', $string);

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

$res = array_combine($explode,$explode);

echo '<pre>';
print_r($res);
echo '</pre>';

Or if you need to make lower case key for resulting array use following :

echo '<pre>';
print_r(array_change_key_case($res,CASE_LOWER));
echo '</pre>';
0
On
<?php
$string = 'Apple, Orange, Lemone';
$array = explode(', ', $string);
print_r($array);

output will be

Array
(
    [0] => Apple
    [1] => Orange
    [2] => Lemone
)

Now

$ar = array();
    foreach ($array as $value) {
        $ar[$value] = $value;
    }

print_r($ar);

Your Desire Output:

Array
(
    [Apple] => Apple
    [Orange] => Orange
    [Lemone] => Lemone
)
1
On
$valuesInArrayWithSpace = explode("," $string);

$finalArray = [];

foreach ($ValuesInArrayWitchSpace as $singleItem) {

    $finalArray[trim(strtolower($singleItem))] = trim($singleItem);
} 
2
On

use array_flip to change the values as key and use array_combine

<?php
     $string = 'Apple, Orange, Lemone';
     $array = explode(', ', $string);

     $new_array =array_flip($array);
     $final_array = array_combine($new_array,$array)
     print_r($final_array );

?>
0
On

Or...

$csvdata = str_getcsv('Apple,Orange,Lemone');
$arr = [];

foreach($csvdata as $a) {
    $arr[strtolower(trim($a))] = $a;
}

print_r($arr);
1
On

This way you'll have what you need:

$lowerString = strtolower($string);
$values = explode(', ', $string);
$keys   = explode(', ', $lowerString);
$array  = array_combine($keys, $values);
print_r($array);
0
On
<?php 
$string = 'Apple, Orange, Lemone'; // your string having space after each value
$string =str_replace(' ', '', $string); // removing blank spaces
$array = explode(',', $string );
$final_array = array_combine($array, $array);
$final_array = array_change_key_case($final_array, CASE_LOWER); // Converting all the keys to lower case based on your requiment 
echo '<pre>';
print_r($final_array);

?>
3
On

You can use array functions such as array_combine, and array_values

$string = 'Apple, Orange, Lemone';

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

$assocArr = array_change_key_case(
  array_combine(array_values($arr), $arr)
);
0
On

A clean, functional-style approach can use array_reduce() after splitting on commas followed by spaces.

Before pushing the new associative values into the result array, change the key to lowercase.

Code: (Demo)

$string = 'Apple, Orange, Lemone';
var_export(
    array_reduce(
        explode(', ', $string),
        fn($result, $v) =>
            $result + [strtolower($v) => $v],
        []
    )
);

Output:

array (
  'apple' => 'Apple',
  'orange' => 'Orange',
  'lemone' => 'Lemone',
)