我有两个不同的数组,格式如下:
$customersList = Array(
Array('id' => null, 'title' => 'JOHN LEGEND'),
Array('id' => null, 'title' => 'MARIAH CAREY'),
Array('id' => null, 'title' => 'JAY Z'),
Array('id' => null, 'title' => null),
);
$searchingValue = Array(
Array('lastName' => 'TRAVOLTA', 'firstName' => 'JOHN'),
Array('lastName' => 'CAREY', 'firstName' => 'MARIAH'),
Array('lastName' => 'MODE', 'firstName' => 'DEPECHE'),
Array('lastName' => 'BRUCE', 'firstName' => 'WILL'),
);
我希望根据以下算法将$customersList
的值与来自$searchingValue
的值进行匹配:if((firstName1 == $ firstName 2 and $ lastName1 == $ lastName2)或(firstName1 == lastName2或lastName1 == firstName2)为true,则不执行任何操作在$customersList
中插入新客户。
我的代码是:
$ignoreIfFound = true;
foreach ($searchingValue as $value) {
$uploadedAlready = false;
foreach ($customersList as $info) {
if ($info['id'] === null and $info['title']) {
list($paxLastName, $paxFirstName) = explode(' ', $info['title']);
if (($paxLastName == $value['lastName'] and $paxFirstName == $value['firstName']) or ($paxLastName == $value['firstName'] and $paxFirstName == $value['lastName'])) {
if (!$ignoreIfFound and !$uploadedAlready) {
$customersList[] = Array('id' => null, 'title' => $value['lastName'].' '.$value['firstName']);
$uploadedAlready = true;
}
}
else {
if (!$uploadedAlready) {
$customersList[] = Array('id' => null, 'title' =>$value['lastName'].' '.$value['firstName']);
$uploadedAlready = true;
}
}
}
else {
if (!$uploadedAlready) {
$customersList[] = Array('id' => null, 'title' => $value['lastName'].' '.$value['firstName']);
$uploadedAlready = true;
}
}
}}
但它无法正常工作。对于给定的示例,新的customersList应该是:
$customersList = Array(
Array('id' => null, 'title' => 'JOHN LEGEND'),
Array('id' => null, 'title' => 'MARIAH CAREY'),
Array('id' => null, 'title' => 'JAY Z'),
Array('id' => null, 'title' => null),
Array('id' => null, 'title' => 'TRAVOLTA JOHN'),
Array('id' => null, 'title' => 'MODE DEPECHE'),
Array('id' => null, 'title' => 'BRUCE WILL'),
);
提前谢谢!
答案 0 :(得分:0)
构建所有已创建的密钥(名字,姓氏和名字以及姓氏)的地图并检查:
<?php
$alreadyInList = array ();
foreach ( $customersList as $customer) {
$alreadyInList[$customer["title"]] = true;
$firstLastname = explode(" ", $customer["title"]);
$alreadyInList[$firstLastname[0]] = true;
$alreadyInList[$firstLastname[1]] = true;
}
foreach($searchingValue as $search) {
if (array_key_exists($alreadyInList, $search["lastname"]))
continue;
if (array_key_exists($alreadyInList, $search["firstname"]))
continue;
if (array_key_exists($alreadyInList, $search["firstname"] . " " . $search["lastname"]))
continue;
$customersList[] = new array() // ... insert
$alreadyInList[$search["firstname"] . " " . $search["lastname"]] = true;
$alreadyInList[$search["lastname"]] = true;
$alreadyInList[$search["firstname"]] = true;
}