Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
Just use a foreach to loop over $arr2, and for each element, loop over $arr1 to get the element(s) you want. –  Rocket Hazmat Jul 1 '14 at 19:14

3 Answers 3

up vote 1 down vote accepted

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
                )

        )
)
share|improve this answer
    
This is exactly what I was looking to do. I'm new to programming and find wrapping my head around nested loops, especially foreach loops, is a bit difficult. Thank you! –  user3642863 Jul 2 '14 at 18:25

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'];
}
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.