poleposters Posted August 26, 2010 Share Posted August 26, 2010 Hi, I need some help getting my head around arrays. I have a basket of fruit: $basket=("Apple","$Orange","$Banana","$Banana","$Peach","$Apple","$Banana","$Orange","$Apple"); I want to distribute and share this basket of fruit into bowls, such that each bowl contains the following: $bowl=("$Apple",$Orange,"$Banana","$Banana") ie. One Apple, One Orange & Two Bananas. I can't seem to figure out how to do it. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/ Share on other sites More sharing options...
AbraCadaver Posted August 26, 2010 Share Posted August 26, 2010 You only have enough fruit for one complete bowl. What do you want to do with the extra items? After the first bowl you will only have one banana , so do you want a second bowl with an apple an orange and a banana and then a third with only an apple? Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/#findComment-1103993 Share on other sites More sharing options...
poleposters Posted August 27, 2010 Author Share Posted August 27, 2010 Sorry, I should of mentioned that. I want ignore pears and peaches. Where there is not enough fruit for a complete bowl. I'd like a NULL value in place of the missing fruit. Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/#findComment-1104216 Share on other sites More sharing options...
Andy-H Posted August 27, 2010 Share Posted August 27, 2010 <?php $basket = array('$Apple','$Orange','$Banana','$Banana','$Peach','$Apple','$Banana','$Orange','$Apple'); $order = array('$Apple','$Orange','$Banana','$Banana'); function filter($arrSort, $arrOrder) { $arrSort = array_unique($arrSort); foreach($arrSort as $val) { if (in_array($val, $arrOrder)) { return true; } else { continue; } } return false; } function sortFruit(&$basket, $order) { $bowl = array(); $index = 0; while(filter($basket, $order)) { for ($i = 0; $i < count($order); $i++) { if ( in_array($order[$i], $basket) ) { $bowl[$index][] = $order[$i]; unset($basket[array_search($order[$i], $basket)]); } else { $bowl[$index][] = 'NULL'; } } $index++; } return $bowl; } echo 'Basket:<br >'; echo '<pre>' . print_r($basket, true) . '</pre><br ><br >'; echo 'Order:<br >'; echo '<pre>' . print_r($order, true) . '</pre><br ><br >'; $bowl = sortFruit($basket, $order); echo 'Ordered Basket(s):<br >'; echo '<pre>' . print_r($bowl, true) . '</pre><br ><br >'; ?> Long winded but does the trick. Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/#findComment-1104227 Share on other sites More sharing options...
poleposters Posted August 27, 2010 Author Share Posted August 27, 2010 That's perfect thank you! I'm excited to study it and try and understand how it works. Arrays still boggle my mind. Thank you for your help. Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/#findComment-1104246 Share on other sites More sharing options...
poleposters Posted August 29, 2010 Author Share Posted August 29, 2010 After a day of studying the code above I've finally figured out how it all works. But now I've encountered a new problem. How could I modify the code to allow me to store some information about each piece of fruit? e.g red apple, green apple, yellow banana This would not affect the way the fruit is sorted into bowls. A red or green apple could go into the bowl interchangeably. I would just like to be able to access the bowl array to determine whether the apple is green or red. Thanks! Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/#findComment-1104869 Share on other sites More sharing options...
poleposters Posted August 30, 2010 Author Share Posted August 30, 2010 Also, if it makes it any easier. I'll be filtering the initial array in the sql query to remove peaches & pears etc. Link to comment https://forums.phpfreaks.com/topic/211792-fun-with-arrays-distribute-an-array-into-groupssets/#findComment-1105146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.