0

I have string

$string1 = `a,b,c,d`;

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

Gives me :

array(
    (int) 0 => 'a',
    (int) 1 => 'b',
    (int) 2 => 'c'
    (int) 3 => 'd'
)

But I want it to be like this

array(
        'a' => 'a',
        'b' => 'b',
        'c' => 'c'
        'd' => 'd'
    )

How do I do that

2 Answers 2

3

Use array_combine function

$string = `a,b,c,d`;
$array = explode(',', $string);    
var_dump(array_combine($array, $array));
Sign up to request clarification or add additional context in comments.

1 Comment

This is probably a better solution then mine above.
1

I think you have to create a new array after exploding...

$tmp_arr = explode(',', $string1);
$array1 = array();
foreach ($tmp_arr as $item){
    $array1[$item] = $item;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.