soma56 Posted June 30, 2010 Share Posted June 30, 2010 I'm seriously spinning here. I have multiple arrays that I need echoed pending certain conditions. I've created this fictitious example in a feeble attempt to convey what I'm trying to do: <?PHP $ladies = explode("\n", $_POST['ladies']); $shoes = explode("\n", $_POST['shoes']); foreach($ladies as $lady){ foreach($_POST['mall'] as $store){ foreach($shoes as $shoe) { $buyshoes = 1; $size = "soldout"; while ($size != "soldout"): $buyshoes = $buyshoes +1; endwhile; } } } ?> Basically, I need every single lady to buy every type of shoe from every store until their size is soldout. What's making my head spin is that, for arguments sake let's say the first two ladies in the first array are "Dorothy" and "Elaine". How do I tell Dorothy that her size is out and then immediately start the process all over again with Elaine while going to the first value in the second array (or first store in Mall)? In this example: Dorothy goes to every store and buys shoes until here size is no longer available. Elaine starts at the very first store (first value in $store) and goes through the same process, etc. etc. Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/ Share on other sites More sharing options...
soma56 Posted June 30, 2010 Author Share Posted June 30, 2010 I think an 'if' statement is in order. Here's (hopefully) a little more clarification: <?PHP $buyshoes = 1; $ladies = explode("\n", $_POST['ladies']); $shoes = explode("\n", $_POST['shoes']); foreach($_POST['mall'] as $store){ foreach($ladies as $lady) { foreach($shoes as $shoe) { $size = 'sale'; $pos = strpos($manager,$sale); if($pos === false) { $buyshoes = $buyshoes + 1; $page = function($ContunueShoppingFirstLady);{ } else { //Continue Cycling Through foreach with next lady } } } ?> What's happening now is that the number of shoes is being bought is increase ($buyshoes + 1) however it jumps from the first value in the first array ($lady) to the next one in line. What I'm trying to accomplish is the first value in "$lady" to "$buyshoes + 1" until "$pos" is false. and THEN proceed to the next value (lady) in the array. Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/#findComment-1079058 Share on other sites More sharing options...
soma56 Posted June 30, 2010 Author Share Posted June 30, 2010 I think I have it. I just need to figure out how to reverse/step back one value in an array. Is there a simple function that will do this? Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/#findComment-1079061 Share on other sites More sharing options...
harristweed Posted June 30, 2010 Share Posted June 30, 2010 http://php.net/manual/en/control-structures.continue.php Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/#findComment-1079074 Share on other sites More sharing options...
soma56 Posted June 30, 2010 Author Share Posted June 30, 2010 Thank you. I've looked over the continue function. I can't see how it will help with my problem. <?PHP $buyshoes = 1; $size = 'null'; $manager = 'null'; $pos = true; $ladies = explode("\n", $_POST['ladies']); foreach($ladies as $lady){ foreach($_POST['mall'] as $store){ $size = 'sale'; $manager = 'possibly talking about sale'; $pos = strpos($manager,$sale); if($pos === false) { $buyshoes = $buyshoes +1; echo $buyshoes; $lady--; //This is where I need help (How to remain within first value? $firstlady = function($ContunueShoppingFirstLady);{ } else { //Continue Cycling Through foreach with next lady //Next lady in $ladies array to first $store } } } ?> I don't need to 'continue' but rather remain within the first value or 'array1' while exhausting all the values of 'array2'. I think 'continue' may be the opposite of what I need. If a certain condition exists - my code doesn't remain at the first value but rather goes to the next one. 'value2'. An idiotic metaphorical example: Currently: Group of ladies => Dorothy => Goes to a every store and buys only one pair at sale price Group of ladies => Elaine => Goes to a every store and buys only one pair at sale price Group of ladies => Judy => Goes to a every store and buys only one pair at sale price Ideally: Group of ladies => Dorothy => Goes to a every store and buys every pair at sale price Group of ladies => Elaine => Goes to a every store and buys every pair at sale price Group of ladies => Judy => Goes to a every store and buys every pair at sale price Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/#findComment-1079197 Share on other sites More sharing options...
KevinM1 Posted June 30, 2010 Share Posted June 30, 2010 The easiest way to do this would be with objects. Something like: class Lady { private $name; private $shoeSize; private $shoes = array(); public function __construct($name, $shoeSize) { $this->name = $name; $this->shoeSize = $shoeSize; } public function getName() { return $this->name; } public function getShoeSize() { return $this->shoeSize; } public function buyShoe(Shoe $shoe) { $this->shoes[] = $shoe; } } class Shoe { private $size; public function __ construct($size) { $this->size = $size; } public function getSize() { return $this->size; } } class Mall { private $shoes = array(); public function addShoe(Shoe $shoe) { $this->shoes[] = $shoe; } public function visit(Lady $lady) { foreach ($this->shoes as $shoe) { if ($shoe->getSize() == $lady->getShoeSize()) { $lady->buyShoe($shoe); } } } } $Dorothy = new Lady("Dorothy", ; // build our lady $mall = new Mall(); // generic mall for ($i = 0; $i < 20; ++$i) { $shoe = new Shoe(/* random size */); // build a random shoe $mall->addShoe($shoe); // add it to our mall } $mall->visit($Dorothy); // the mall is visited by Dorothy, who buys all shoes in her size Obviously, it's just a rough sketch, but this is how I'd approach it. Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/#findComment-1079268 Share on other sites More sharing options...
soma56 Posted June 30, 2010 Author Share Posted June 30, 2010 Thank you Nightslyr. I'm going to have to take a step back and review arrays and classes. I appreciate everyone's help! Quote Link to comment https://forums.phpfreaks.com/topic/206257-ok-arrays-are-starting-to-make-my-head-spin-now/#findComment-1079284 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.