Jump to content

Ok, Arrays are starting to make my head spin now...


soma56

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.