i have this kind of array:
$array[] = "BRAND A|PERIOD 1|RANGE 1";
$array[] = "BRAND A|PERIOD 1|RANGE 2";
$array[] = "BRAND A|PERIOD 1|RANGE 3";
$array[] = "BRAND A|PERIOD 2|RANGE 1";
$array[] = "BRAND A|PERIOD 2|RANGE 2";
$array[] = "BRAND A|PERIOD 2|RANGE 3";
$array[] = "BRAND A|PERIOD 3|RANGE 1";
$array[] = "BRAND A|PERIOD 3|RANGE 2";
$array[] = "BRAND A|PERIOD 3|RANGE 3";
$array[] = "BRAND B|PERIOD 1|RANGE 1";
$array[] = "BRAND B|PERIOD 1|RANGE 2";
$array[] = "BRAND B|PERIOD 1|RANGE 3";
and I would like to get the following result:
BRAND A: [PERIOD 1] [PERIOD 2] [PERIOD 3]
BRAND B: [PERIOD 1]
I tried to use a for loop, with which listing and explode all values, then array_unique to remove duplicate in $brand and $period array values, finally two foreach nested cycles... But I can't get a better result than this:
BRAND A: [PERIOD 1] [PERIOD 2] [PERIOD 3]
BRAND B: [PERIOD 1] [PERIOD 2] [PERIOD 3]
The "Brand B" array contains only a unique value: Period 1, but the script prints all unique values in the "Brand A" array.
This is my code:
for($i = 0; $array[$i]; $i++):
list($brand, $period, $range) = explode("|", $array[$i]);
$array_brand[] = $brand;
$unique_brand = array_unique($array_brand);
$array_period[] = $period;
$unique_period = array_unique($array_period);
endfor;
foreach($unique_brand as $value_brand):
echo $value_brand . ": ";
foreach($unique_period as $value_period):
echo "[" . $value_period . "] ";
endforeach;
echo "<br />";
endforeach;
Any suggestion to get the desired result is very welcome. Thanks in advance.