首页 > 解决方案 > 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.

标签: phpcodeigniter-3

解决方案


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

推荐阅读