kakes Posted February 4, 2011 Share Posted February 4, 2011 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/ Share on other sites More sharing options...
.josh Posted February 4, 2011 Share Posted February 4, 2011 so what's your question? Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1169677 Share on other sites More sharing options...
kakes Posted February 4, 2011 Author Share Posted February 4, 2011 My code doesn't work and I don't know what I did wrong. :'( Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1169682 Share on other sites More sharing options...
.josh Posted February 4, 2011 Share Posted February 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1169686 Share on other sites More sharing options...
kakes Posted February 4, 2011 Author Share Posted February 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1169699 Share on other sites More sharing options...
kakes Posted February 4, 2011 Author Share Posted February 4, 2011 I'm still working on this. I think it's in my foreach statement but I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1169880 Share on other sites More sharing options...
.josh Posted February 4, 2011 Share Posted February 4, 2011 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 --- Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1169891 Share on other sites More sharing options...
kakes Posted February 6, 2011 Author Share Posted February 6, 2011 Thanks. Maybe I can figure out the rest. You've been a big help and I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/226627-associative-array-help-please/#findComment-1170496 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.