jcanker Posted October 9, 2011 Share Posted October 9, 2011 I have an associative array that is serialized and stored in a MySQL DB. The array is a duty roster for my son's Cub Scout camping trip (adult volunteers for specific jobs). When a user clicks a spot on the jQuery/client side, it sends it to my php page via ajax, which should pull up the array from the DB, unserialize it, replace the value "Available" with the person's name for the date/time/job that they clicked on. For ease of processing, I need to reference the value to replace by numerical indexes rather than the associative key string-type name, but when I try it, it just says Undefined offset. I thought I could access key/values using the numerical index in square brackets, just as I could if it were a non-associative array. What gives? (You can see near the end where I tried to access the array by index; this now-commented out line just tacked it on at the end, like it was recognizing the digits as strings instead of integers. I've tried to cast the values as integers before putting them in brackets, but same effect. // get the duty roster array from the database $query = "select dutyroster from ".$eventsTable." where eventID = 1"; $result = mysql_query($query); if(!$result) { } else { //put the returned result into a usable array $returnedRows = mysql_fetch_array($result,MYSQL_BOTH); $unserializedArray = unserialize($returnedRows[0]); } //get the returned values for ID, email, and fillerValue $ID = "0-0-2-2";//$_POST['ID']; $email = "[email protected]";//$_POST['email']; $fillerValue = "Dude (test)";//$_POST['fillerValue']; // parse $ID to break out the array index & set them as variables $explodedArray = explode("-",$ID); $iDate = (int)$explodedArray[0]; $iTimeblock = (int)$explodedArray[1]; $iJob = (int)$explodedArray[2]; $iSlot = (int)$explodedArray[3]; echo "<br/> the values are: idate: ".$iDate." and iTimeblock: ".$iTimeblock." and iJob: ".$iJob." and iSlot: ".$iSlot."</br>"; echo "<BR/> the random selected value to display is: ".$unserializedArray[0]; //$unserializedArray[$iDate][$iTimeblock][$iJob][$iSlot] = $fillerValue; print_r($unserializedArray); Quote Link to comment https://forums.phpfreaks.com/topic/248727-php-associative-array-referenced-by-numerical-index/ Share on other sites More sharing options...
Buddski Posted October 9, 2011 Share Posted October 9, 2011 You cannot use numeric keys to access an associative array. You can however determine a number using array_keys() This will give you an array with numerical keys with each value containing the associative key. (if that makes sense) Example: $input = array('something'=>'element 1','buddski'=>'element 2','details'=>'element 3'); $array = array_keys($input); echo '<pre>',print_r($array,true),'</pre>'; /* Array ( [0] => something [1] => buddski [2] => details ) */ Quote Link to comment https://forums.phpfreaks.com/topic/248727-php-associative-array-referenced-by-numerical-index/#findComment-1277380 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.