Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组中的值与另一个数组中的值匹配_Php_Arrays - Fatal编程技术网

Php 将数组中的值与另一个数组中的值匹配

Php 将数组中的值与另一个数组中的值匹配,php,arrays,Php,Arrays,我有两个不同的数组,格式如下: $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),

我有两个不同的数组,格式如下:

$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
中的值进行匹配:如果((firstName1=$firstName2和$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;

        }
    }
}}
但是它不能正常工作。例如,新的CustomerList应该是:

$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'),
);

提前感谢!

构建所有已建立密钥(Firstname、Lastname和First+Lastname)的地图,并检查:

<?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;
}

通过使用数据库,这应该更容易些。您会得到哪种输出?我对$ignoreIfFound=true的输出:是一个数组,其中包含searchingValue中的所有值,附加在CustomerList上,这是不正确的。非常感谢,我用请求的算法重写了此代码:)