Reputation:
$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?
Upvotes: 3
Views: 164
Reputation: 3163
Use a foreach
loop on the explode to add them:
foreach($explode as $key) {
$array["content"][$key] = "NULL";
}
Upvotes: 0
Reputation: 1425
$input = "hello|world|look|at|this";
$explode = explode('|', $input);
$nulls = array();
foreach($explode as $x){ $nulls[] = null; };
$array = array("Title" => "Hello!", "content" => array_combine($explode, $nulls));
Upvotes: 0
Reputation: 1300
how about array_flip($explode)
? That should give you this
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[hello]=> 1
No null
values but atleast you got the keys right
Upvotes: 0
Reputation: 22656
array_fill_keys
can populate keys based on an array:
array_fill_keys ($explode, null);
Upvotes: 2