skip to Main Content

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


  1. As record is unique, I recommend rebuilding $arr using this value as the key. Then you loop again to replace your value for $sponsor

    $indexedArray = [];
    foreach ($array as $v) {
      $indexedArray[$v['record']] = $v;
    }   
    foreach ($indexedArray as $record => $v) {
      if (isset($indexedArray[$v['sponsor']]) && isset($indexedArray[$v['sponsor']]['email'])) {
        $indexedArray[$record]['sponsor'] = $indexedArray[$v['sponsor']]['email'];
      }
    }
    $yes = array_values($indexedArray);
    
    Login or Signup to reply.
  2. Create a lookup array with record values as keys and email values as values, then overwrite the sponsor value in each row based on the corresponding lookup value.

    Code: (Demo)

    $lookup = array_column($array, 'email', 'record');
    var_export(
        array_map(fn($row) => array_replace($row, ['sponsor' => $lookup[$row['sponsor']]]), $array)
    );
    

    Or with a classic loop: (Demo)

    $lookup = array_column($array, 'email', 'record');
    foreach ($array as &$row) {
        $row['sponsor'] = $lookup[$row['sponsor']];
    }
    var_export($array);
    

    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 checking isset().


    p.s. Bear in mind that using the array union operator won’t work properly in this case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search