0

This is driving me nuts.

I'm attempting to reada CSV file (done) and then work through the permutations of each row. Each row contains several bits of data (name, price etc.). Some of them contain slash separated lists (a/b/c/c3/c65).

What I need to do is generate all the possible variations of each row.

Example:

Row 12 = 
    Name = name, 
    Price = price, 
    Models = x12/x14/x56,
    Codes = LP1/LP12/LP899/XP90/XP92,        

From that I should be able to generate 15 variations, each with the same Name and Price, but with different Codes and varied Models;
Name Price X12 LP1
Name Price X12 LP12
Name Price X12 LP899
~
Name Price X56 XP90
Name Price X56 XP92

Yet I'm either overwriting pre-existing versions, or generating individual versions, but only getting 1 set of values changing (so I may get the 15 versions, but only Model changes, everything else stays the same).

Any help/thoughts or pointers would be appreciated!

theclueless1
  • 123
  • 1
  • 1
  • 11

2 Answers2

0

So you have one row containing that much items, say

$row =    array('Name'=>'name', 'price'=>'price','models'=>'x12/x14/x56','codes'=>'LP1/LP12/LP899/XP90/XP92')

and you want to split models and codes with "/" then have each item as a new row in the array with all the columns those having the same value for price and name field, here is how you can do this,

$line = 0;
$result_array = array();
$result_array[$line]['name'] = $row['name'];
$result_array[$line]['price'] = $row['price'];

//split the models using explode
$tmpModels = explode("/",$row['models']);
foreach($tmpModels as $mod){
  if($line > 0){
    $result_array[$line]['name'] = $row['name'];
    $result_array[$line]['price'] = $row['price'];
  }
  $result_array[$line]['model'] = $mod;
  $line++;
}

$line = 0;
//now split the codes using explode
$tmpCodes = explode("/",$row['models']);
foreach($tmpCodes as $cod){
  $result_array[$line]['code'] = $cod;
  $line++;
}

if(count($tmpCodes) > count($tmpModels)){ // then few more rows should be added to include all from codes
   foreach($tmpCodes as $cod){
    $result_array[$line]['name'] = $row['name'];
    $result_array[$line]['price'] = $row['price]'
    $result_array[$line]['model'] = '';
    $result_array[$line]['code'] = $cod;
    $line++;
  }
}

$result_array will have what you want.

This code is not tested, so there can be some errors, btw i hope this will surely give you an idea on how to achieve that.

Robin
  • 854
  • 8
  • 20
0

Let's say you have array that looks like this:

$variant=Array();

$list[0]=array('Name'=>'Item name', 'Price'=>'$400','Models'=>'x12/x14/x56','Codes'=>'LP1/LP12/LP899/XP90/XP92');
$list[1]=array('Name'=>'Item name', 'Price'=>'$400','Models'=>'x12/x14/x56','Codes'=>'LP1/LP12/LP899/XP90/XP92'); // and more array.......
for($i=0;$i<count($list);$i++){
    $Names=$list[$i]["Name"];
    $Prices=$list[$i]["Price"];
    $Models=explode("/",$list[$i]["Models"]);
    $Codes=explode("/",$list[$i]["Codes"]);
    for($i2=0;$i2<count($Codes);$i2++){
        $variant[]=Array("name"=>$Names,"price"=>$Prices,"model"=>$Models[0],"code"=>$Codes[$i2]);
        $variant[]=Array("name"=>$Names,"price"=>$Prices,"model"=>$Models[1],"code"=>$Codes[$i2]);
        $variant[]=Array("name"=>$Names,"price"=>$Prices,"model"=>$Models[2],"code"=>$Codes[$i2]);

        // You can add more models by copy paste it and change $Models[2] with next available $Models array index
    }
}
var_dump($variant);
?>

The results will produce 30 array, because we have 2 rows, so that's not wrong ... okay

Reason for looping the codes

Because codes is more greater than models. So, we can catch all values. Good luck, btw i have test it and that's worked

Mike
  • 1,231
  • 12
  • 17