I have created an array in PHP of strings. While my understanding of PHP is imperfect, I believe that arrays are by default ordered by an index and I should be able to access an element of the array using: array[0] or array[1] etc. Regardless, I have created the array by explicitly creating an index with the following code employing a loop:
//create empty array container
$myarray = "";
//create counter
$i = 0;
//begin loop of limited size...
$myarray[$i] = "some color...this changes with each iteration of loop";
$i = $i + 1;
//end loop
Note I end up with something like 1=>red,2+>blue,3+>green etc.
I know this code works because it creates an array that if I implode, gives me the string that I want e.g. redbluegreen
$arrayasstring = implode("",$myarray);//produces redbluegreen
However, I would like to access one of the values of the loop using the index and change it. For example, I might want to change $array[0] from red to purple.
$index = 0;
$myarray[$index] = "purple"; //does not work
What is a simple way to accomplish this?
Thanks for any suggestions.
$myarray = "";creates a string, not an array.$myarray, but then you use$arrayin the call toimplode().