Appending a new value to an associative array

Hi,

As part of a large application I have an existing array with key value pairs such as:

    $scores2 = array(
    array(
        'player' => 'chris',
        'sheet' => '9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,0'
    ),
    
);

This array will be saved in a session.

I then receive new data for ‘chris’ in the form of a new array:

    $scores2 = array(
    array(
        'player' => 'chris',
        'sheet' => '1,2,3,4,5'
    ),
    
);

So i need to append the new data ‘1,2,3,4,5’ to the original array to end up with:

$scores2 = array(
    array(
        'player' => 'chris,
        'sheet' => '9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,0,1,2,3,4,5'
    ),
    
);

I thought this was possible with array_merge but apparently it is not as i cannot get it to work. Any suggestions?

I haven’t tested this out on multiplayers or a mix mash, but maybe something like the following?

<?php
$old_scores = array(
	array(
		'player' => 'chris',
		'sheet' => '9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,0'
		),
	);

$new_scores = array(
	array(
		'player' => 'chris',
		'sheet' => '1,2,3,4,5'
		),

	);

echo '<pre>';
print_r($old_scores);
echo '</pre>';
echo '<pre>';
print_r($new_scores);
echo '</pre>';



foreach ($old_scores as $old_data_set) {
	foreach ($new_scores as $new_data_set) {
		if ($old_data_set['player'] === $new_data_set['player']) {
			$new_data_set['sheet'] = $old_data_set['sheet'] . ',' . $new_data_set['sheet'];
		}
	}
}

echo '<pre>'.print_r($new_data_set,1).'</pre>';

I’m sure someone here will come up with a fancier way to do this. :wink:

This should do it with multiple updates as well as a single update.

<?php 


function add_scores($old,$new){
    $newscores = array();
    $updated_players = array();
    foreach($new as $arr1):
        foreach($old as $k => $arr2):        
            if($arr2['player'] === $arr1['player']){
                $newscores[$k]['player'] = $arr2['player'];
                $newscores[$k]['sheet'] = $arr2['sheet'] . ',' .$arr1['sheet'];
                $updated_players[] = $k;
            }elseif(!in_array($k,$updated_players)){
                $newscores[$k] = $arr2;
            }    
        endforeach;
    endforeach;
    return $newscores;
}


$oldscores = array(
    0 => array(
        'player' => 'chris',
        'sheet' => '9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,9,1,0'
    ),
    1 => array(
        'player' => 'sam',
        'sheet' => '5,1,6,1,9,5,8,9,9,4,9,1,5,1,7,1,8,1,3,1,2'
    ),
    2 => array(
        'player' => 'john',
        'sheet' => '4,1,6,1,9,5,8,9,9,4,4,1,5,1,2,1,7,1,3,6,5'
    ),
    3 => array(
        'player' => 'gary',
        'sheet' => '7,1,5,1,5,5,8,2,9,4,4,1,5,1,2,1,7,1,3,6,5'
    )

);


$addscores = array(
    0 => array(
        'player' => 'chris',
        'sheet' => '1,2,3,4,5'
    ),    
    1 => array(
        'player' => 'john',
        'sheet' => '2,4,5,4,1'
    ),    
    2 => array(
        'player' => 'gary',
        'sheet' => '5,4,3,2,1'
    )

); 


$newscores = add_scores($oldscores,$addscores);

echo '<pre>';
    print_r($newscores);
    print_r($oldscores);
    print_r($addscores);
echo '</pre>';
?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.