Firstly, I'm assuming that you are passing in the array shown at the top of your question. Which will therefore be in the form:
$array = [
['AA' => 111],
['AB' => 111],
['AC' => 111],
['AD' => 111],
['AE' => 111],
['AA' => 112]
];
Assuming that that is correct then your if statement does not work:
$array[0] == $source_id && $array[1] !== $source_number
// $array[0] && $array[1] simply don't exist! Referencing them should
// throw a notice in PHP...
// In the first foreach loop $array looks as follows
$array = ['AA' => 111];
// In the second loop it will be
$array = ['AB' => 111];
// and so on through the array. The index, in this case, is AA, AB, AC etc.
// the indexes 0 and 1 just don't exist
That being the case we can begin to fix your code by modifying the if statement to:
key($array) == $source_id && current($array) !== $source_number
Of course we also need to update these two lines:
foreach ($array1 as $array)
$array1[1] = $source_number;
To:
foreach ($array1 as &$array)
$array = [$source_id => $source_number];
So, your code is now partially working; if we call the function it will update existing ID values however it won't add new ones...
// Notice we pass $array by reference( & ) so that it updates the original array
// if you prefer not to do this then, obviously, you need to return the updated array
function addToken(&$array, $source_id, $source_number)
{
if (!empty($array)) {
foreach ($array as &$arr) {
if(key($arr) == $source_id && current($arr) != $source_number) {
$arr = [$source_id => $source_number];
}
}
}
}
addToken($array, "AB", 112);
// Updated array...
$array = [
['AA' => 111],
['AB' => 112],
['AC' => 111],
['AD' => 111],
['AE' => 111],
['AA' => 112]
];
Adding an item if it doesn't exist
To do this we need to firstly look though the entire array to see if there is a match and if there isn't, at the end, then we add a new item.
function addToken(&$array, $source_id, $source_number)
{
if(!empty($array)){
$tokenExists = false;
foreach($array as &$arr){
if(key($arr) == $source_id){
$arr = [$source_id => $source_number];
$tokenExists = true;
}
}
if(!$tokenExists){
$array[] = [$source_id => $source_number];
}
}
}
Notice, we don't need to check whether the value matches $source_number, we just update the value to $source_number either way we have the same result.
However unless you have duplicates in your input array (as shown, purposefully in the example I've used above) where an ID in the original array could have multiple values that all need to be updated to the new one. There is no need for any of this @TP has the right idea!
Simplify the format to:
$array = [
'AA' => 111, // Note this value is effectively overwritten by the last line of the arrray, you can't have duplicate keys
'AB' => 111,
'AC' => 111,
'AD' => 111,
'AE' => 111,
'AA' => 112
];
function addToken(&$array, $source_id, $source_number)
{
$array[$source_id] = $source_number;
}
Although, with this simplified array... Do you even need to put the update in a function/method? I guess that depends on the rest of your code.