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 explode function like this

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

that gives me this- Array ( [0] => apple [1] => orang )

Somebody says this question is a duplicate of this SO question: How can I split a comma delimited string into an array in PHP?

that question not addressing my problem, I have already reached the answer of that question, please read my question. I needed to change that result arry's key value. see

Array
(
    [0] => 9
    [1] => admin@example.com
    [2] => 8
)

to like this

Array
(
    [9] => 9
    [admin@example.com] => admin@example.com
    [8] => 8
)
Community
  • 1
  • 1
Salih K
  • 631
  • 2
  • 11
  • 27

8 Answers8

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
  • 2,882
  • 2
  • 30
  • 35
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,038
  • 2
  • 21
  • 61
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
  • 2
  • 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. Abu Taleb
  • 1,556
  • 1
  • 13
  • 23
1
$valuesInArrayWithSpace = explode("," $string);

$finalArray = [];

foreach ($ValuesInArrayWitchSpace as $singleItem) {

    $finalArray[trim(strtolower($singleItem))] = trim($singleItem);
} 
omxv
  • 428
  • 4
  • 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,512
  • 13
  • 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
  • 634
  • 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,793
  • 1
  • 10
  • 25
  • 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