I have code like bellow
$string = "Trainee,Beginner";
I want to replace the $string to array object with explode
$list = explode(',', $string);
The result I got.
array:2 [▼
0 => "Trainee"
1 => "Beginner"
];
The result I want.
array:2 [▼
'Trainee' => "Trainee"
'Beginner' => "Beginner"
];
You can do it with array_combine()
that takes one array as key and another as value. So just pass the $list
for both parameters and you're good to go.
<?php
$string = "Trainee,Beginner";
$list = explode(',', $string);
$final_array = array_combine($list, $list);
print_r($final_array);
?>
DEMO: https://3v4l.org/vmgaH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With