Jump to content

PHP associative array Referenced by numerical index


jcanker

Recommended Posts

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 = "blank@blank.com";//$_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);

Link to comment
Share on other sites

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
)
*/

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.