0

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.

7
  • That should work. What's the difference from your original code in the loop? Commented Aug 4, 2021 at 16:19
  • $myarray = ""; creates a string, not an array. Commented Aug 4, 2021 at 16:19
  • Your loop is modifying $myarray, but then you use $array in the call to implode(). Commented Aug 4, 2021 at 16:20
  • 1
    All these snippets with different variables are confusing, it's hard to tell what you're doing wrong. Please post a minimal reproducible example. Commented Aug 4, 2021 at 16:21
  • I corrected the myarray vs. array conflict which was a typo. Commented Aug 4, 2021 at 20:16

1 Answer 1

1

To initialize an Array you have to use [] or the function array:

In your example:

$myarray = array();

If you want to add a series of values with the default key assignment you only have to put in between the parenthesis: array(color1, color2, color3)

$myarray = array('blue', 'red', 'yellow')

Then you can view what is on your variable, using: print_r($myarray)

print_r($myarray)
// this will output:
Array
(
    [0] => blue
    [1] => red
    [2] => yellow
)

After this, now you can change the key => value, as you want:

$index = 0;
$myarray[$index] = "purple";
print_r($myarray);

//this will now output:
Array
(
    [0] => purple
    [1] => red
    [2] => yellow
)

And your job is done.

Observations:

  • In your question, you are using two differents arrays: $myarray and $array, you are having an error because $array[$index] is different to the first $myarray[$i] = "some color...this changes with each iteration of loop";

  • Use print_r($yourVariable) to show what is in the variable to debug what you are doing wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

Not initializing array properly may have been the main issue. Also I was using var_dump which just shows string and number of characters instead of print_r. Using print_r helped me to debug.
Using $myarray = array('blue', 'red', 'yellow'); you don't need to initialize the array, since you are creating it with values. There's another way that you can initialize it: $myArray = []; $myArray = array(); $myArray = (array) null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.