doubledee Posted April 25, 2011 Share Posted April 25, 2011 I've never been very good with arrays, and am totally new to them in PHP... :-\ If I have this array... $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); ...and I know the key, e.g. $concertDate = '201105072' Then how do I get the corresponding value, i.e. May 7, 2011 (1:00pm - 4:00pm)' ...from the array?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/ Share on other sites More sharing options...
kenrbnsn Posted April 25, 2011 Share Posted April 25, 2011 <?php echo $concertDates[$concertDate]; ?> or <?php $var = $concertDates[$concertDate]; echo $var; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206135 Share on other sites More sharing options...
trq Posted April 25, 2011 Share Posted April 25, 2011 echo $concertDates['201105072']; Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206136 Share on other sites More sharing options...
doubledee Posted April 25, 2011 Author Share Posted April 25, 2011 What does this mean??? Notice: Uninitialized string offset: 201105072 in /Users/user1/Documents/DEV/++htdocs/00_MyWebSite/payment_form4_auth4.php on line 394 $concertDate = $concertDates = ''; $orderDescription = ''; Line 394 // Create Order Description. $orderDescription = "Rockin' with Beethoven, "; $orderDescription .= "$concertDates[$concertDate] "; $orderDescription .= "Tempe High School, Tempe, AZ"; Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206146 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 It means that $concertDates is a string, not an array. Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206158 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 It means that $concertDates is a string, not an array. Is that because I initially said... concertDates = ''; Because it clearly is an array... $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206162 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 Yeah, when you do the $concertDates = ''; it reinitializes it as an empty string instead of an array. If the array is defined and you comment out the offending line, it should work. Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206165 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 Yeah, when you do the $concertDates = ''; it reinitializes it as an empty string instead of an array. If the array is defined and you comment out the offending line, it should work. But I initialized the array - obviously - before I populated it?! How do you declare and initialize an empty array in PHP? And what is the proper way to initialize variables in general?? I always get errors in my programs about "undefined/undeclared variable" so I just start every script now like this... $firstName = $lastName = ''; $address1 = $address2 = $fullAddress = ''; $city = $state = $zip = ''; BTW, these are arrays... $months = $years = ''; ...and I didn't have any issues with them when I did this... $months = array(1=>'01','02','03','04','05','06','07','08','09','10','11','12'); $years = range(2011, 2020); Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206169 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 To initialize an empty array, you'd simply do this $empty = array(); Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206176 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 At the top of my script I changed things to... $concertDate = ''; $concertDates = array(); And farther down I still have... // Create Order Description. $orderDescription = "Rockin Beethoven, "; $orderDescription .= $concertDates[$concertDate] . " "; $orderDescription .= "Our High School, Anytown, USA"; But I am still getting an error... Notice: Undefined index: 201105141 in /Users/user1/Documents/DEV/++htdocs/00_MyWebSite/payment_form4_auth4.php on line 397 (Line 397 is the middle one above.) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206178 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 Well, let's see what's in the array then. It may not be populating as you think it is. $orderDescription = "Rockin Beethoven, "; $orderDescription .= $concertDates[$concertDate] . " "; $orderDescription .= "Our High School, Anytown, USA"; echo '$concertDates:<br><pre>'; print_r($concertDates); echo '</pre>'; // <--- ADD THIS LINE Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206183 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 Well, let's see what's in the array then. It may not be populating as you think it is. $orderDescription = "Rockin Beethoven, "; $orderDescription .= $concertDates[$concertDate] . " "; $orderDescription .= "Our High School, Anytown, USA"; echo '$concertDates:<br><pre>'; print_r($concertDates); echo '</pre>'; // <--- ADD THIS LINE I get this... $seminarDates: Array ( ) So why is my array empty??? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206196 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 Without seeing the code that populates it, there's no way to answer that question. Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206198 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 Without seeing the code that populates it, there's no way to answer that question. But I already gave you the code snippet. (I can't post all of my code here due to size and the fact that it have a lot of sensitive info in it like Payment Gateway account info.) This should be enough to help me figure this out... Starting from the top down... <body> <?php // Initialize variables. $attendeeName = $venue = ''; $venues = array(); $concertDate = ''; $concertDates = array(); $invoiceNumber = $orderDescription = ''; $merchantContact = $receiptMessage = ''; $errors = array(); // ********************************************************************* // HANDLE FORM. // ********************************************************************* if (isset($_POST['submitted'])){ // Trim all incoming data. $trimmed = array_map('trim', $_POST); // ****************************** // CHECK CONCERT INFORMATION. * // ****************************** // Check Concert Date. if (!empty($_POST['concertDate'])){ if (preg_match('#^(201105071|201105072|201105141)$#', $_POST['concertDate'])){ $concertDate = $_POST['concertDate']; }else{ $errors['concertDate'] = 'Choose a Date from the drop-down list.'; } }else{ $errors['concertDate'] = 'Please select a Concert Date.'; } Then later on my form.... <!-- Concert Date --> <li> <label for="concertDate">Concert Date/Time:</label> <select id="concertDate" name="concertDate"> <option value=""></option> <!-- Create concert Date Select List --> <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); foreach($concertDates as $key => $value){ echo '<option value="' . $key . '" '; echo (!empty($_POST['concertDate']) && $_POST['concertDate']==$key) ? 'selected="selected"' : ''; echo '>' . $value . '</option>\n'; } ?> </select> <?php if (!empty($errors['concertDate'])){ echo '<span class="error">' . $errors['concertDate'] . '</span>'; } ?> </li> The rest of this form/script works fine.. :'( Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206209 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 I put test echos in my code. // Check Concert Date. if (!empty($_POST['concertDate'])){ if (preg_match('#^(201105071|201105072|201105141)$#', $_POST['concertDate'])){ $concertDate = $_POST['concertDate']; }else{ $errors['concertDate'] = 'Choose a Date from the drop-down list.'; } }else{ $errors['concertDate'] = 'Please select a concert Date.'; } echo "concertDate = " . $concertDate; Yields... concertDate = 201105072 ...after I select the 2nd option in the drop-down. <!-- Concert Date --> <li> <label for="concertDate">Concert Date/Time:</label> <select id="concertDate" name="concertDate"> <option value=""></option> <!-- Create concert Date Select List --> <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); echo '$concertDates:<br><pre>'; print_r($concertDates); echo '</pre>'; // <--- ADD THIS LINE foreach($concertDates as $key => $value){ echo '<option value="' . $key . '" '; echo (!empty($_POST['concertDate']) && $_POST['concertDate']==$key) ? 'selected="selected"' : ''; echo '>' . $value . '</option>\n'; } ?> </select> <?php if (!empty($errors['concertDate'])){ echo '<span class="error">' . $errors['concertDate'] . '</span>'; } ?> </li> Yields... Notice: Undefined index: 201105072 in /Users/user1/Documents/DEV/++htdocs/00_MyWebSite/payment_form4_auth4.php on line 399 Call Stack # Time Memory Function Location 1 0.0038 184488 {main}( ) ../payment_form4_auth4.php:0 $concertDates: Array ( ) ...after submitting the form I don't see how the array ($concertDates) can be empty and yet the variable ($concertDate) has a $key value from the array?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206214 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); This is the actual code that populates the array? The array doesn't come from the result of a database query? Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206215 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); This is the actual code that populates the array? The array doesn't come from the result of a database query? That's it!! (There are only 3 dates and I was supposed to have this done today. No time for a database. If I ever survive this project, then I can look into making it more robust.) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206216 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 If that's all the code you can post, all I can suggest is to print_r() the array at different points in the script until you figure out at which point it loses its values. Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206217 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 If that's all the code you can post, all I can suggest is to print_r() the array at different points in the script until you figure out at which point it loses its values. I initialize at the top of my script... <body> <?php // Initialize variables. $concertDate = ''; $concertDates = array(); // ********************************************************************* // HANDLE FORM. // ********************************************************************* if (isset($_POST['submitted'])){ // Trim all incoming data. $trimmed = array_map('trim', $_POST); // ****************************** // CHECK CONCERT INFORMATION. * // ****************************** // Check Concert Date. if (!empty($_POST['concertDate'])){ if (preg_match('#^(201105071|201105072|201105141)$#', $_POST['concertDate'])){ $concertDate = $_POST['concertDate']; }else{ $errors['concertDate'] = 'Choose a Date from the drop-down list.'; } }else{ $errors['concertDate'] = 'Please select a Concert Date.'; } echo "concertDate = " . $concertDate; }// End of HANDLE FORM. ?> <!-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ --> <!-- HTML PAYMENT FORM --> <form id="payment" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> It looks like I may be erasing out all of my variables every time I re-sumit the form onto itself... Any ideas??? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206223 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 Can someone please help on this?? :'( Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206239 Share on other sites More sharing options...
kenrbnsn Posted April 26, 2011 Share Posted April 26, 2011 Instead of doing this <?php $concertDates = array(); ?> at the start of your script, initialize the array with the values and remove all other lines that populate the array: <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206277 Share on other sites More sharing options...
doubledee Posted April 26, 2011 Author Share Posted April 26, 2011 Instead of doing this <?php $concertDates = array(); ?> at the start of your script, initialize the array with the values and remove all other lines that populate the array: <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); ?> Ken My problem was I *thought* the array variable stayed alive when I re-submitted the form. It doesn't. (Wasted 8 hours on this!) Am now frantically trying to re-learn Sessions to help me out... Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206285 Share on other sites More sharing options...
viviosoft Posted April 26, 2011 Share Posted April 26, 2011 Debbie, I would take a step back before looking into sessions to solve your problem. When someone clicks Submit on your form are they going to another page in your app / site? If so then the array needs to be used on the second page. If not and you are just refreshing the page on submit then the array should still be "alive". Wherever you have your Array. Use print_r(). <?php $concertDates = array('201105071'=>'May 7, 2011 (9:00am - 12:00pm)', '201105072'=>'May 7, 2011 (1:00pm - 4:00pm)', '201105141'=>'May 14, 2011 (9:00am - 12:00pm)'); print_r($concertDates); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/234713-reading-array-to-get-value/#findComment-1206470 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.