How to generate an array with values without these being inside an index subarray 0 in PHP?

1

With this code:

$template["letras"][] = $letras;
$template["animales"][] = $animales;

$template = array(
    "letras" => array("a"=>1, "b"=>2, "c"=>3), //sin comillas al cierre
    "animales" => array("leon"=>1, "vaca"=>2, "cabra"=>3)
    );

foreach ($template as $features => $features_value) {
    ${$features."_inner"} = $features_value;
}

It generates an array like this:

$letras_inner = array(0 => $features_value);
// $letras_inner = array(0 => array(3) => array("a"=>1, "b"=>2, "c"=>3))

I hope to get this:

$letras_inner_inner = $features_value;
// $letras_inner = array("a"=>1, "b"=>2, "c"=>3))

How can I make the assigned array not become a subarray of index zero, but save it directly?

    
asked by Victor Alvarado 19.03.2018 в 15:39
source

1 answer

0

Reviewing the sandbox published by @ LeonardoCabré in the comments of my question, notice that the code works correctly.

I decided to check $ features_value well and this was the one that was being obtained as an array of index 0 and corresponding to that index the value of the array.

Check the code where I get the value of $ letters and $ animals and I was creating an array where it was a normal variable, this is reviewed thinking about Sasha's comment about copying variables in PHP.

The code stayed like this:

$template["letras"] = $letras;
// Sin corchetes

$template["animales"] = $animales;
// Sin corchetes

$template = array(
    "letras" => array("a"=>1, "b"=>2, "c"=>3), //sin comillas al cierre
    "animales" => array("leon"=>1, "vaca"=>2, "cabra"=>3)
    );

foreach ($template as $features => $features_value) {
    ${$features."_inner"} = $features_value;
}
    
answered by 19.03.2018 в 16:12