Search code examples
phparrayskey-value-store

How to store an array as a value of a key in another array in PHP?


I am trying to store an array as a value of a key in another array. I can't use functions like array_push() or a for loop because the second array is being created as a parameter of a function.

I've googled for hours and can't find any answers! I'm new to PHP so I have no idea if this is even possible.

The first array is something like:

    $choices[] = array('1'=>'Blue','2'=>'Red)

EDIT: this is the code I meant to add

    $choices[] = array('1'=>'Blue','2'=>'Red');

The lack of ' and ; were a typo. The array is being created in a much more complex way. In my attempt to abstract and simplify it to post it here I missed the ' and ; . Kudos to @diéfani-favareto-piovezan for spotting that.

Now I want to insert this array as a value of a key in another array that is being created in another part of the code:

     return randomFunction('random text', array(
                                   'a' => 'apple',
                                   'b' => 'banana',
                                   'c' =>  ,
                                  )

                          )

I want array $choices to be the value of key 'c'.

I tried this

     return randomFunction('random text', array(
                                   'a' => 'apple',
                                   'b' => 'banana',
                                   'c' => $choices,
                                  ),

                          )

but it gives me an error

EDIT: I meant to say that I tried this

     return randomFunction('random text', array(
                                   'a' => 'apple',
                                   'b' => 'banana',
                                   'c' => $choices,
                                  )

                          )

Once again, when trying to abstract and simplify (the actual array created in the RandomFunction has about 250 lines) I added the extra comma by mistake. Kudos to @brevis, @Nick and @tim for spotting this and bringing it to my attention.

    Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /filename.php line 120

The error mentions another line down the code, but I know that it is not the cause because if I remove

                                   'c' => $choices,

there is no error.

EDIT: I meant to say "before I added the 'c' line" (and not "if I remove it" ), there were no errors. I also could have added that the creation of $choices comes before the randomFunction array, so that's another reason why I knew there were no errors there.

CONCLUSION: As I mentioned, the error was an extra ), after the 'c' line, which I failed to reproduce here when trying to simplify the code. I think that when I added the 'c' line in my actual code I must have added an extra ), by mistake. I was only able to find that because @nick said that the randomFunction code (the one after "I tried this") should be working. That was the answer I needed to confirm that the error was not on that line. Thanks to to @dont-panic's comment, I looked more carefully into the error message I was getting and found the extra )'. At the end of the day, eveybody helped me. So thank you all!

To anyone who has come across this, the working code is:

    $choices[] = array('1'=>'Blue','2'=>'Red');

     return randomFunction('random text', array(
                                   'a' => 'apple',
                                   'b' => 'banana',
                                   'c' => $choices,
                                  )

                          );

*


Solution

  • ok, I found the error:

    I was adding an extra ), at the end of the second array.

    The working code is

    $choices[] = array('1'=>'Blue','2'=>'Red');
    
     return randomFunction('random text', array(
                                   'a' => 'apple',
                                   'b' => 'banana',
                                   'c' => $choices,
                                  )
    
                          );