2

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] => admin@example.com
    [2] => 8
)

to like this

Array
(
    [9] => 9
    [admin@example.com] => admin@example.com
    [8] => 8
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Salih K
  • 701
  • 2
  • 12
  • 31

9 Answers9

3

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>';
Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
3
<?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);

?>
Mittul At TechnoBrave
  • 1,142
  • 3
  • 25
  • 70
3

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)
);
Hamed Ghaderi
  • 100
  • 1
  • 10
2
<?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
)
Md. Abutaleb
  • 1,590
  • 1
  • 14
  • 24
1
$valuesInArrayWithSpace = explode("," $string);

$finalArray = [];

foreach ($ValuesInArrayWitchSpace as $singleItem) {

    $finalArray[trim(strtolower($singleItem))] = trim($singleItem);
} 
omxv
  • 616
  • 6
  • 11
1

Or...

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

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

print_r($arr);
John Corry
  • 1,567
  • 12
  • 15
1

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);
Vinicius Dias
  • 664
  • 3
  • 15
0

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 );

?>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • this will give you Array ( [Apple] => 0 [Orange] => 1 [Lemone] => 2 ) – Salih K Dec 29 '16 at 11:19
  • Not only is this snippet missing a semicolon, it does not provide the desired output. https://3v4l.org/AZ9V4 If this was the desired output, you would only call `explode()` and be done with it. This answer can be safely removed. – mickmackusa Mar 10 '21 at 03:57
0

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',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136