Question

$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 available?

Était-ce utile?

La solution 2

Use a foreach loop on the explode to add them:

foreach($explode as $key) {
    $array["content"][$key] = "NULL";
}

Autres conseils

array_fill_keys can populate keys based on an array:

array_fill_keys ($explode, null);

how about array_flip($explode)? That should give you this

array(2) {
 ["Title"]=>
 string(6) "Hello!"
   ["content"]=>
    array(5) {
      [hello]=> 1

No nullvalues but atleast you got the keys right

$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));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top