TonEUK Posted April 2, 2007 Share Posted April 2, 2007 I have an array of numbers which can be different everytime. For example I have the numbers 7 8 20 5. How can I loop through the array so that the numbers are paried together like below. 7, 8 7, 20 7, 5 8, 7 8, 20 8, 5 20, 7 20, 8 20, 5 5, 7 5, 8 5, 20 The reason I want this to happen is because I have a shopping basket of product IDs and I want all the above valuse to be stored in a history table. So if product 7 and 8 were bought at the same time then they would be paired in the list. Thanks TonE Link to comment https://forums.phpfreaks.com/topic/45344-php-looping-help/ Share on other sites More sharing options...
dustinnoe Posted April 2, 2007 Share Posted April 2, 2007 Why not create a relationship id for all related numbers and then tie each number to that relationship id. So if your relationship id equals 1 all you would need is 1, 7 1, 8 1, 20 1, 5 Some of these numbers could also be related to another set 2, 7 2, 20 2, 2 2, 38 Now all those numbers are related with much less processing and storage. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/45344-php-looping-help/#findComment-220194 Share on other sites More sharing options...
Barand Posted April 2, 2007 Share Posted April 2, 2007 <?php $prods = array (7, 8, 20, 5); $copy = $prods; foreach ($prods as $a) { foreach ($copy as $b) { if ($a != $b) { echo "$a : $b <br>"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/45344-php-looping-help/#findComment-220196 Share on other sites More sharing options...
dough boy Posted April 3, 2007 Share Posted April 3, 2007 I guess I do not see the need for it. Are you already storing all of the items per transaction in a table? You should be able to use a query to determine what other items they bought, i.e. when looking at product 7, you could say what other items people bought. Is this the intent? Link to comment https://forums.phpfreaks.com/topic/45344-php-looping-help/#findComment-220216 Share on other sites More sharing options...
dustinnoe Posted April 3, 2007 Share Posted April 3, 2007 Are you already storing all of the items per transaction in a table? You should be able to use a query to determine what other items they bought, i.e. when looking at product 7, you could say what other items people bought. Is this the intent? That's essentially the same point I was trying to make in so many words. Link to comment https://forums.phpfreaks.com/topic/45344-php-looping-help/#findComment-220409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.