显示可以合并、为空且存在于一个数组中的内容

Show what could be merged, is empty, and exists in one array

我正在尝试从 table 中取出两条记录并比较它们以确定哪些可以被视为合并冲突,哪些需要忽略,哪些可以自动合并而不会发生冲突。

我有一个客户 table 并且想做以下事情:

我想获取此信息并将其传递到一个视图,在该视图中我可以显示控件以让用户决定要做什么。

我想出了以下方法,但我对 PHP 的数组函数不是很好,也许可以使用更好或更有效的方法?

$results = [];
foreach ($client_first as $key => $value){
    if (empty($value) && empty($client_second[$key])) {
        $results['ignore'][] = $key;
    } elseif (!empty($value) && empty($client_second[$key])) {
        $results['merged'][$key] = $value ? $value : $client_second[$key];
    } elseif (empty($value) && !empty($client_second[$key])) {
        $results['merged'][$key] = $value ? $value : $client_second[$key];  
    } elseif (!empty($value) && !empty($client_second[$key])) {
        $results['conflicts'][] = $key;
    }
}

我没有和上面的结婚。因此,欢迎提出任何建议。

这两个数组类似于:

第一个数组:

array:8 [
  first_name => "John"
  last_name => "Doe"
  middle_initial => null
  email => null
  cell_phone => null
  education_level => null
  gender => "Male"
  race => "White"
]

第二个数组:

array:8 [
  first_name => "Johnn"
  last_name => "Does"
  middle_initial => null
  email => null
  cell_phone => null
  education_level => null
  gender => null
  race => null
]

我希望看到:

array:3 [
  "conflicts" => array:2 [
    0 => "last_name"
    1 => "first_name"
  ]
  "ignore" => array:4 [
    0 => "middle_initial"
    1 => "email"
    2 => "cell_phone"
    3 => "education_level"
  ]
  "merged" => array:2 [
    "gender" => "Male"
    "race" => "White"
  ]
]

输入:

$a1=[
  'first_name' => "John",
  'last_name' => "Doe",
  'middle_initial' => null,
  'email' => null,
  'cell_phone' => null,
  'education_level' => null,
  'gender' => "Male",
  'race' => "White"
];
$a2=[
  'first_name' => "Johnn",
  'last_name' => "Does",
  'middle_initial' => null,
  'email' => null,
  'cell_phone' => null,
  'education_level' => null,
  'gender' => null,
  'race' => null
];

方法:

$result=["conflicts"=>[],"ignore"=>[],"merged"=>[]];    
foreach($a1 as $k=>$v){
    if($v==$a2[$k]){                       // no change, ignore
        $result["ignore"][]=$k;
    }elseif(is_null($v)){                  // yes change, 1st is null, use 2nd
        $result["merged"][$k]=$a2[$k];
    }elseif(is_null($a2[$k])){             // yes change, 2nd is null, use 1st
        $result["merged"][$k]=$v;
    }else{                                 // neither are null, user decides
        $result["conflicts"][]=$k;
    }
}
var_export($result);

输出:

array (
  'conflicts' => 
  array (
    0 => 'first_name',
    1 => 'last_name',
  ),
  'ignore' => 
  array (
    0 => 'middle_initial',
    1 => 'email',
    2 => 'cell_phone',
    3 => 'education_level',
  ),
  'merged' => 
  array (
    'gender' => 'Male',
    'race' => 'White',
  ),
)

此外,您可以使用以下方法存储实际冲突:

$result["conflicts"][]=["field"=>$k,"firstvalue"=>$v,"secondvalue"=>$a2[$k]];

在 else 块中。这会给你一个这样的冲突子数组:

'conflicts' => array (
    0 => array (
      'field' => 'first_name',
      'firstvalue' => 'John',
      'secondvalue' => 'Johnn',
    ),
    1 => array (
      'field' => 'last_name',
      'firstvalue' => 'Doe',
      'secondvalue' => 'Does',
    )
  )

此数据存储结构可能会帮助您设置用户界面。