user3642863
user3642863

Reputation: 45

PHP Replace Array Values

I have 2 multidimensional arrays that I am working with:

$arr1 = 
Array
([type] => characters
 [version] => 5.6.7.8
 [data] => Array
           ([Char1] => Array
                    ([id] => 1
                     [name] =>Char1
                     [title] =>Example
                     [tags] => Array
                            ([0] => DPS
                             [1] => Support))
            [Char2] => Array
                    ([id] => 2
                     [name] =>Char2
                     [title] =>Example
                     [tags] => Array
                            ([0] => Tank
                             [1] => N/A)
                    )
            )

etc...

$arr2=
Array
([games] => Array
         ([gameId] => 123
          [gameType => Match
          [char_id] => 1
          [stats] => Array
                  ([damage] => 55555
                   [kills] => 5)
         )
         ([gameId] => 157
          [gameType => Match
          [char_id] => 2
          [stats] => Array
                  ([damage] => 12642
                   [kills] => 9)
         )

etc...

Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.

I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.

Thanks for any help or suggestions!

Upvotes: 3

Views: 1840

Answers (3)

user1978142
user1978142

Reputation: 7948

Actually, this is quite straighforward. (I don't think there a built-in function that does this.)

Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)

Consider this example:

// your data
$arr1 = array(
    'type' => 'characters',
    'version' => '5.6.7.8',
    'data' => array(
        'Char1' => array(
            'id' => 1,
            'name' => 'Char1',
            'title' => 'Example',
            'tags' => array('DPS', 'Support'),
        ),
        'Char2' => array(
            'id' => 2,
            'name' => 'Char2',
            'title' => 'Example',
            'tags' => array('Tank', 'N/A'),
        ),

    ),
);

$arr2 = array(
    'games' => array(
        array(
            'gameId' => 123,
            'gameType' => 'Match',
            'char_id' => 1,
            'stats' => array('damage' => 55555, 'kills' => 5),
        ),
        array(
            'gameId' => 157,
            'gameType' => 'Match',
            'char_id' => 2,
            'stats' => array('damage' => 12642, 'kills' => 9),
        ),
    ),
);

foreach($arr2['games'] as &$value) {
    $arr2_char_id = $value['char_id'];
    // loop and check against the $arr1
    foreach($arr1['data'] as $element) {
        if($arr2_char_id == $element['id']) {
            $value['name'] = $element['name'];
        }
    }
}

echo '<pre>';
print_r($arr2);

$arr2 should look now like this:

Array
(
    [games] => Array
        (
            [0] => Array
                (
                    [gameId] => 123
                    [gameType] => Match
                    [char_id] => 1
                    [stats] => Array
                        (
                            [damage] => 55555
                            [kills] => 5
                        )

                    [name] => Char1 // <-- name
                )

            [1] => Array
                (
                    [gameId] => 157
                    [gameType] => Match
                    [char_id] => 2
                    [stats] => Array
                        (
                            [damage] => 12642
                            [kills] => 9
                        )

                    [name] => Char2 // <-- name
                )

        )
)

Upvotes: 1

Patrick Q
Patrick Q

Reputation: 6393

If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.

foreach($arr2['games'] as $key => $innerArray)
{
    $arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}

Upvotes: 0

ssergei
ssergei

Reputation: 1299

Iterate over $arr2 and add the data to it from the matching $arr1 array value:

$i = 0;
foreach($arr2['games'] as $arr2Game){
      $id = $arr2Game['char_id'];
      $arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
      $i++;
 }

Have not tested this code.

Upvotes: 0

Related Questions