0

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.

  • I don't really understand the logic behind how you want to form your result? – Jim Wright Jul 22 '17 at 02:16
  • @JimWright I'm assuming he wants to show the periods based on the brand, but only once to ensure no duplicates make it in. –  Jul 22 '17 at 02:37
  • The result I have already achieved generates empty values => empty links => so opens empty pages without products. I would like that if a period does not exist in a specific product life cycle (As is the case in the BRAND B), it should not be shown :) – J.Sepulvedas Jul 22 '17 at 02:40

2 Answers2

0

I haven't tested this code, but I think it gives you what you want.

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

$res = [];
foreach ($array as $str) {
    $items = explode('|', $str);
    if (! array_key_exists($items[0], $res)) {
        $res[$items[0]] = [];
    }
    if (! in_array($items[1], $res[$items[0]])) {
        $res[$items[0]][] = $items[1];
    }
}
foreach ($res as $k => $v) {
     sort($res[$k]);
      echo $k . ": ";

     foreach ($v as $y){
        echo "[" . $y . "] ";
     }
 echo "<br />";
 }
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
  • 1
    Thank you very much, Jim. Your code works perfectly, I just corrected some simple syntax errors and added a second foreach loop to get the exact result I was looking for. – J.Sepulvedas Jul 22 '17 at 14:35
0

You can do this using one loop.

$brands = [
    'a' => [],
    'b' => []
];

foreach($array as $arr) {
    $rowData = explode('|', $arr);
    if($rowData[0] == 'BRAND A' && !in_array($rowData[1], $brands['a'])) {
        array_push($brands['a'], $rowData[1]);
    }
    if($rowData[0] == 'BRAND B' && !in_array($rowData[1], $brands['b'])) {
        array_push($brands['b'], $rowData[1]);
    }

}

This will give you the result:

$brands = [
    'a' => [
        'PERIOD 1',
        'PERIOD 2',
        'PERIOD 3'
    ],
    'b' => [
        'PERIOD 1'
    ]

];
  • Thank you very much, spencdev. Your code is working too and you were very kind. I was looking for a solution that would not explicitly use "Brand A" or "Brand B" in the If cycle, because the array could virtually continue with "Brand C", "Brand D", "...", so the $brand variable need to be defined automatically :) – J.Sepulvedas Jul 22 '17 at 14:34