Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add explode array as keys to new array

$input = "hello|world|look|at|this";
$explode = explode("|", $input);
$array = array("Title" => "Hello!", "content" => $explode);

This will output:

array(2) {
  ["Title"]=>
  string(6) "Hello!"
  ["content"]=>
  array(5) {
    [0]=>
    string(5) "hello"
    [1]=>
    string(5) "world"
    [2]=>
    string(4) "look"
    [3]=>
    string(2) "at"
    [4]=>
    string(4) "this"
  }
}

But I want them to be keys with a NULL as value as I add values in a later step.

Any idea how to get the explode() function to return as keys? Is there a function from php available?


1 Answers

array_fill_keys can populate keys based on an array:

array_fill_keys ($explode, null);
like image 97
Jim Avatar answered Dec 05 '25 23:12

Jim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!