0

My goal is to be able to update a key value inside of an array inside of an array and I'm don't know if I'm using the right php array function.

BEFORE:

array:2 [
    "week_number" => 1
    "games" => array:1 [
        0 => array:3 [
            "game_number" => 1
            "umpires" => []
            "teams" => []  
        ]
    ]
]

AFTER:

array:2 [
    "week_number" => 1
    "games" => array:1 [
        0 => array:3 [
            "game_number" => 1
            "umpires" => []
            "teams" => [1,2]  
        ]
    ]
]

Test Class:

private function validParams($overrides = [])
{
    return array_merge_recursive([
        'week_number' => 1,
        'games' => [[
            'game_number' => 1,
            'umpires' => [],
            'teams' => [], 
        ]]
    ], $overrides);
}


$response = $this->actingAs($this->authorizedUser)
                    ->post(route('games.store', ['week' => $this->week->id]), $this->validParams([
                        'games' => [][
                            [
                                'teams'  => [1,2]
                            ]
                        ]
                    ]));
5
  • Possible duplicate of In PHP, how do you change the key of an array element? Commented Jun 9, 2018 at 17:35
  • Can you include an example of what the array looks like before and after Commented Jun 9, 2018 at 17:41
  • 1
    I did for you above. Commented Jun 9, 2018 at 17:45
  • Problem solved? Commented Jun 9, 2018 at 23:50
  • I just posted the solution I went with. Commented Jun 10, 2018 at 12:17

3 Answers 3

0

If you want to update the keys... typing $array['new_key'] = $array['old_key'] will duplicate the value with 2 sets of keys.

You have a few options here. Either you create a new array and just set your desired keys or work with array_keys and array_values and mix them up... your choice

http://php.net/manual/en/ref.array.php

See the list above, there are a lot of array functions you can use... see the two above and array_map... there is virtually a great number of ways you can do this. See how your problem is best handled after reviewing the documentation.

Good luck!

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

4 Comments

So you are saying that I can't replace the existing key value.
Not literally replace, you just have to process the data differently in order to obtain your desired result. Simplest way is to do a foreach on the new array, have your conditions there and populate a new array with the new keys, using the values from the initial array. That's how i'd do it, i guess
Is it because I have an array inside of an array because I have other tests that work fine with just array_merge with setting values.
Without a proper example i can't help more... however i suggest going and digging into the solutions provided by me and others and learn how arrays work... you can easily replace values, but to add new keys it will also keep old value, there is no real way to "change" a key, that is how it is designed
0

This is the moment where you need unset(): Adding a value with a different key will not update or overwrite the old value but simply add another key-value pair. Hence, add the new value fist, then unset the old one. We can use To array_walk to itterate over the array:

array_walk($array, function (& $item) {
   $item['new_key'] = $item['old_key'];
   unset($item['old_key']);
});

Take note of the & reference operator in the lambda function: it ensures we are working on the original array and not a copy of it.

2 Comments

This can lead to the interesting problem that if you change a key to an already existing key that this value will be overwritten and then possibly copied when the overwritten item is renamed. See the duplicate flagged for more problems.
Yes, this may be a too simplistic solution if the order, existing keys, and other things have to be taken into account.
0

I found this as a solution.

private function validParams($overrides = [])
{
    return array_replace_recursive([
        'week_number' => 1,
        'games' => [
            0 => [
                'game_number' => 1,
                'umpires' => [],
                'teams' => [],
            ]
        ]
    ], $overrides);
}


->post(route('games.store', ['week' => $this->week->id]), $this->validParams([
    'games' => [
        0 => [
            'teams'  => [1,2]
        ]
                        ]
    ]));

Comments

Your Answer

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