I have an array like so:
$array = [
['record' => 1, 'sponsor' => 2, 'email' => '[email protected]'],
['record' => 2, 'sponsor' => 2, 'email' => '[email protected]'],
['record' => 3, 'sponsor' => 2, 'email' => '[email protected]'],
['record' => 4, 'sponsor' => 2, 'email' => '[email protected]'],
];
Each row has a unique record
and email
and the sponsor
key is related to the record
. So, instead of an integer, I am trying to replace the value of the sponsor
key with the corresponding email
based on the record
, something like this:
$array = [
['record' => 1, 'sponsor' => '[email protected]', 'email' => '[email protected]'],
['record' => 2, 'sponsor' => '[email protected]', 'email' => '[email protected]'],
['record' => 3, 'sponsor' => '[email protected]', 'email' => '[email protected]'],
['record' => 4, 'sponsor' => '[email protected]', 'email' => '[email protected]'],
];
I have tried using a foreach
loop but it doesn’t give me the expected result:
$yes = [];
foreach ($array as $key => $arr) {
if ( ! empty($arr['sponsor']) ) {
if ( $arr['sponsor'] == $array[$key]['record'] ) {
$yes[] = ['sponsor' => $array[$key]['email']];
}
}
$yes[] = [
'record' => $array[$key]['record'],
'email' => $array[$key]['email'],
'sponsor' => $array[$key]['sponsor'],
];
}
print_r($yes);
2
Answers
As
record
is unique, I recommend rebuilding$arr
using this value as the key. Then you loop again to replace your value for$sponsor
–Create a lookup array with
record
values as keys andemail
values as values, then overwrite the sponsor value in each row based on the corresponding lookup value.Code: (Demo)
Or with a classic loop: (Demo)
If there is a chance that a given
sponsor
value might not be found in the lookup, then use$lookup[$row['sponsor']] ?? 'some fallback'
when you try to access the lookup array. This way you will not generate warnings/errors. This is a more concise way of checkingisset()
.p.s. Bear in mind that using the array union operator won’t work properly in this case.