It's probably not possible, and I'm damn fool. But, I am trying to build an array in PHP, where some of the elements are variables.
$myArr = array();
$i = 1;
$n = 5;
for($i = 1; $i <= $n; $i++) {
if($i === 1){
$myArr["k$i"] = array(
'description' => 'Some special text for the k1 element',
'type' => 'some type',
'foo' => 'bar',
)
}else{
$myArr["k$i"] = array(
'description' => 'This is the number ' . $i . ' element description.'
'type' => 'some type',
'foo' => 'bar',
)
}
}
return $myArr;
The results should be:
$myArr = [
k1 => [
'description' => 'Some special text for the k1 element',
'type' => 'some type',
'foo' => 'bar',
],
k2 => [
'description' => 'This is the number 2 element description.'
...
],
] // ending bracket for the $myArr array
PHP complains most often about the curly-bracket closing the IF statement. Any suggestions would be greatly appreciated.
EDIT
The suggestion to look at some long list of 'Common Syntax Errors' is not the answer, and not specific enough of an answer to help me in a timely manner. Also, I simply did not FIND that solution while searching for answers to my question -- perhaps the 'Common Syntax Errors' solution is not properly tagged?
Also, I posted THIS question because the many other questions I managed to find and review, related to PHP arrays, never showed me how to handle variables WITHIN the array. I posted MY question, with example code, in the hopes that a future coder might find how to handle variables WITHIN the array syntax.