php - Using Array in Product Attribute comparison
问题描述
I am doing a product attribute comparison with array:
$array1 = array(
'A' => 'A1',
'B' => 'B1',
'D' => 'D1',
);
$array2 = array(
'A' => 'A2',
'B' => 'B2',
'C' => 'C2',
'D' => 'D2',
'E' => 'E2',
);
What I want to display in Table is:
=====Pdt1=Pdt2=
| A | A1 | A2 |
| B | B1 | B2 |
| C | -- | C2 | <- //put hyphen while no such key and value in $array1
| D | D1 | D2 |
| E | -- | E2 | <- //put hyphen while no such key and value in $array1
===============
I tried to use array_merge_recursive to put 'A1' and 'A2' in a same key 'A', but on 'C' it will only show 'C2' then the result becomes
| C | C2 |
Which I am not able to put in the right column in Table.
解决方案
<?php
$array1 = array(
'A' => 'A1',
'B' => 'B1',
'D' => 'D1',
);
$array2 = array(
'A' => 'A2',
'B' => 'B2',
'C' => 'C2',
'D' => 'D2',
'E' => 'E2',
);
echo "<table border=\"1px solid black;\">";
foreach (range('A', 'Z') as $currentLetter) {
echo "<tr>";
echo "<td>{$currentLetter}</td>";
echo "<td>";
echo (isset($array1[$currentLetter]) ? $array1[$currentLetter] : "-");
echo "</td>";
echo "<td>";
echo (isset($array2[$currentLetter]) ? $array2[$currentLetter] : "-");
echo "</td>";
echo "</tr>";
}
echo "</table>";
推荐阅读
- python-3.x - 在 iPad 上使用 Pyto 运行 Django
- css - 使用 scss @extend 伪选择 CSS
- java - 使用预定义的配置文件运行 Spring Boot JAR
- reactjs - 使用 express 应用程序(生产)构建时不渲染材质 UI 组件
- reactjs - react中的useState没有更新状态对象
- swift - 在 Playground 或 Tricky Way 中调试?
- reactjs - 如何在组件中传递道具?
- angular - 无法在 ionic 3 角度的 html5 播放器中播放本地文件视频
- sql - 多个级联路径 sql server 2017 创建自引用外键
- python - 如何使用 python 在 Atom 上运行程序?