0

I am looping though my data and strong the result in a variable. It is currently being stored as strings.

for ($i = 0; $i < count($AnotherArray); $i++){

  $myArray  .= $dataArray[$i].",";
 }

This correctly returns the content with commas separating the values. But i get a notice

Notice: undefind variable: $myArray in ...

The above is the first time i create and call $myArray

jumpman8947
  • 571
  • 1
  • 11
  • 34

2 Answers2

2

You need to declare the $myArray variable first. So your code becomes:

$myArray = '';
for ($i = 0; $i < count($AnotherArray); $i++){
    $myArray .= $dataArray[$i].",";
}

As a side note, I'd also recommend looking at the naming of your variables, $myArray is actually a string, not an array. Also, $dataArray and $AnotherArray don't describe the data the variables are storing. When coding, it is useful to give variables meaningful names, so that yourself and others who may look at the code will find it easier to follow.

crazyloonybin
  • 935
  • 3
  • 18
  • 36
2

You need to have the variable defined before you can append to it

$myArray="";
for ($i = 0; $i < count($AnotherArray); $i++){

  $myArray  .= $dataArray[$i].",";
 }
Carlos Alves Jorge
  • 1,919
  • 1
  • 13
  • 29