PHP比较CSV制成的多维数组中的键/值

时间:2019-04-30 15:53:38

标签: php arrays csv multidimensional-array

我有两个(2).csv文件,

function disintegrate($elm) { html2canvas($elm).then($canvas => { // create the container we'll use to replace the element with const $container = document.createElement("div"); $container.classList.add("disintegration-container"); // setup the frames for animation const $frames = generateFrames($canvas, NUM_FRAMES); $frames.forEach(($frame, i) => { $frame.style.transitionDelay = `${1.35 * i / $frames.length}s`; $container.appendChild($frame); }); // then insert them into the DOM over the element replaceElementVisually($elm, $container); // then animate them $container.offsetLeft; // forces reflow, so CSS we apply below does transition if (!DEBUG) { // set the values the frame should animate to // note that this is done after reflow so the transitions trigger $frames.forEach($frame => { const randomRadian = 2 * Math.PI * (Math.random() - 0.5); $frame.style.transform = `rotate(${15 * (Math.random() - 0.5)}deg) translate(${60 * Math.cos(randomRadian)}px, ${30 * Math.sin(randomRadian)}px) rotate(${15 * (Math.random() - 0.5)}deg)`; $frame.style.opacity = 0; }); } else { $frames.forEach($frame => { $frame.style.animation = `debug-pulse 1s ease ${$frame.style.transitionDelay} infinite alternate`; }); } }); } /** === Below is just to bind the module and the DOM == */ [...document.querySelectorAll(".disintegration-target")].forEach($elm => { $elm.addEventListener("click", () => { if ($elm.disintegrated) { return; } $elm.disintegrated = true; disintegrate($elm); }); });``` in the short explanation, I want to be able to upload an Image/Video and be able to click it and have it desintegrate, this whole thing is actually for an Avengers: Endgame project Im doing hah!

CompanySubset.csv


| id | company | description | |----|-----------|---------------------------| | 1 | Apple | Description for Apple | | 2 | Microsoft | Description for Microsoft | | 3 | IBM | Description for IBM |

ContactSubset.csv

使用php,我需要读取每个文件,并比较| id | name | address | phone | |----|-------|-------------------|-------------| | 1 | Bob | 1234 Address Lane | 1+234567890 | | 2 | Sally | 4321 Address Lane | 1+987654321 | | 3 | Cam | 2468 Address Lane | 1+468135901 | 列中的两个(2)之间是否匹配。如果我找到了两个(2)之间的匹配ID,则需要将该特定行合并在一起。

Ex: id CompanySubset 1与id ContactSubset 1匹配。因此,在id为1的行上id将与ContactSubset的第1行合并。然后将形成以下形式:

CompanySubset

我已经能够将每个.csv放入一个数组中(见下文),仅此而已。

| id | company | description           | name | address           | phone       |
|----|---------|-----------------------|------|-------------------|-------------|
| 1  | Apple   | Description for Apple | Bob  | 1234 Address Lane | 1+234567890 |

4 个答案:

答案 0 :(得分:1)

您需要使用id元素作为数组的键。然后,您可以合并相关行。

$filename = 'CompanySubset.csv';

$company_array = [];

if ($handle = fopen($filename, "r")) {
    while ($companyData = fgetcsv($handle, 1000, ",")) {
        $company_array[$companyData[0]] = array_slice($companyData, 1);
    }
    fclose($handle);
}

$filename = 'ContactSubset.csv';

$contact_array = [];

if ($handle = fopen($filename, "r")) {
    while ($contactData = fgetcsv($handle, 1000, ",")) {
        $contact_array[$companyData[0]] = array_slice($contactData, 1);
    }
    fclose($handle);
}

有关用相同键合并数组元素的方法,请参见Merging two PHP arrays with same numeric key

也不需要所有这些!== FALSE测试,只需将赋值的值用作条件表达式(有时需要几次,例如测试array_search()的结果,因为0是虚假的,但您的使用均不需要)。

答案 1 :(得分:0)

已使用以下功能:

  

array_walk()-将用户提供的函数应用于数组的每个成员

     

array_merge-合并一个或多个数组

解决方案:

$companySubset = [
  1 => ['Apple','Description for Apple'],
  2 => ['Microsoft','Description for Microsoft'],
  3 => ['IBM','Description for IBM']
];

$contactSubset = [
 1 => ['Bob','1234 Address Lane','1+234567890 '],
 2 => ['Sally','4321 Address Lane','1+987654321'],
 3 => ['Cam','2468 Address Lane','1+468135901']
];
$res =[];
array_walk($companySubset, function($v,$k) use ($contactSubset, &$res){
  $res[] = array_key_exists($k, $contactSubset) ? array_merge($v,$contactSubset[$k]) : $v;
});

答案 2 :(得分:0)

  1. 从csv中创建两个以id为键的二维数组。
  2. 使用https://www.php.net/manual/ru/function.array-intersect-key.php来获取两个ID相交的ID。
  3. 如果有,请遍历步骤2中的一组ID,然后比较实际值。

答案 3 :(得分:0)

这是通过首先读取ContactSubset.csv并创建一个由第一个字段索引的数组(使用array_shift()将其从结果中删除)来解决的。

然后第二个循环实际上已经与您的代码相同,但是在读取每一行时,它会检查联系人详细信息,并使用array_merge()添加额外的详细信息...

$filename = 'ContactSubset.csv';
$contact_array = [];

if (($handle = fopen($filename, "r")) !== FALSE) {
    while (($contactData = fgetcsv($handle)) !== FALSE) {
        $id = array_shift($contactData);
        $contact_array[$id] = $contactData;
    }
    fclose($handle);
}

$filename = 'CompanySubset.csv';
$company_array = [];

if (($handle = fopen($filename, "r")) !== FALSE) {
    while (($companyData = fgetcsv($handle)) !== FALSE) {
        if ( isset($contact_array[$companyData[0]]) )   {
            $companyData = array_merge($companyData, $contact_array[$companyData[0]]);
        }
        $company_array[] = $companyData;
    }

    fclose($handle);
}
print_r($company_array);