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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.