Jump to content

Associative array help, please.


kakes

Recommended Posts

Hello all!!

 

I need help with an associative array I've been working on. I'm not sure if I should have typed the weekday in the index because I'm supposed to use the getdate() function to pull the day of the week into the array. I'm also supposed to use string functions to pull apart the value where it's connected with the colon ":". I've tried using explode() and strtok but I don't quite understand how they work...just like the getdate()..I don't understand. Thanks for any 'nudge' in the right direction you can offer.

 

<?php
$title = "Associative Array";
$heading = "Daily Specials";


$specials = array(
			"Sunday" =>"52in Flat Screen TV:425.00",
			"Monday" =>"Amplifier:145.00",
			"Tuesday" =>"HP Computer:355.00",
			"Wednesday" =>"500GB External Harddrive:99.00",
			"Thursday" =>"Internal Speakers:55.00",
			"Friday" =>"Ergonomic Keyboard:85.00",
			"Saturday" =>"Wacom Tablet:175.00",
			);


echo "<html>
	<head>
		<title>
			$title
		</title>
	</head>
	<body>
		   <h1>
			  $heading
		   </h1>		
		   <pre>\n";
//prints left and right aligned headers with 30 spaces
printf ("%-30s%30s\n", "Items", "Price"); 
printf ("%'-60s\n", "");
foreach ($specials as $key=>$value);
{
//prints the key left aligned and the value right aligned 
//formats to two decimals spaces
printf ("%-20s%20.2f\n", $key, $value);

echo "     </pre>\n"; 
$delimeter=":";
$inventory=strtok($inventory,$delimeter);
   while(is_string($inventory))
   {
if($inventory)
   {
echo "      $inventory\n";
   }
   $inventory=strtok($delimiter);
   }  
$inventory=explode($delimeter,$inventory);
foreach ($items as $item=>$price)
   {
   echo"     $item, $price\n";
   }
echo "     </pre>\n"; 
  "</body>
      </html>\n";
?>

Link to comment
Share on other sites

That reminds me of the time my car was acting weird so I called up the mechanic and asked him what was wrong with my car and he said "Well what is it doing?" and I said "Well it's acting weird.." and he said...

 

"Are you a fucking idiot? How am I supposed to know what is wrong with your car when instead of bringing it in for me to look at it, you call me up on the phone, and when I ask you what's wrong with it, you seem to think that "It's acting weird" will totally help me identify the problem!  Seriously, if you want me to fix your car, bring it in so I can take a look at it.  Or at least describe what you think is wrong, what you expect and what it is not doing (or doing) instead."

 

True story

 

And the moral of the story is, you may not know how to fix your code, but you should know what it is supposed to be doing and what it is (or is not) doing instead, and I can't help you unless you tell me these things.

Link to comment
Share on other sites

Sorry. My array does not display like it should in a browser. I need it to display the day of the week, the item and the price. I need to explode the array where the item and price is joined by a colon ":". I don't think I have the explode() quite right. Would someone mind taking a look? There are other issues, also, but I'm such a noobie and have no idea what the issues could be.

 

I hope this explains better what I need help with. Thank you again.

Link to comment
Share on other sites

Example:

 

$specials = array(
            "Sunday" =>"52in Flat Screen TV:425.00",
            "Monday" =>"Amplifier:145.00",
            "Tuesday" =>"HP Computer:355.00",
            "Wednesday" =>"500GB External Harddrive:99.00",
            "Thursday" =>"Internal Speakers:55.00",
            "Friday" =>"Ergonomic Keyboard:85.00",
            "Saturday" =>"Wacom Tablet:175.00",
            );

foreach ($specials as $day => $special) {
  list($product, $price) = explode(':',$special);

  // variables
  // $day     : Sunday, etc...
  // $product : 52in Flat Screen TV, etc..
  // $price   : 425.00, etc...  	


  // example
  echo $day . "<br/>";
  echo $product . "<br/>";
  echo $price . "<br/>";
  echo "---<br/>";
} // end foreach $specials

 

output:

 

Sunday
52in Flat Screen TV
425.00
---
Monday
Amplifier
145.00
---
Tuesday
HP Computer
355.00
---
Wednesday
500GB External Harddrive
99.00
---
Thursday
Internal Speakers
55.00
---
Friday
Ergonomic Keyboard
85.00
---
Saturday
Wacom Tablet
175.00
---

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.