Unexpected values in an associative array



Hi

I think this is probably just a misunderstanding on my part, but I'm creating associative arrays with string keys from MySQL query results and when I put a value in the array, I get the expected key association along with an index key that has a different value.

For example: If I have a table "Foo," do a select, perform coersions on the results, and place the coerced value in an array with a key, a second uncoerced value is also placed in the array with an index key. I know all associative arrays have both key and index accessors, but I would think that the values should be the same.

Here's the full function.
(Note: The line where values are added to the array is: "$fields [$key] = $value;" after the "coersions" switch statement)

function query_database($inQuery)
{
$query = $inQuery;
$coersions = null;
$object_key = null;
$group_by_key = null;

if (is_array($inQuery))
{
$query = $inQuery['query'];
$coersions = $inQuery['coersions'];
$object_key = $inQuery['object_key'];
$group_by_key = $inQuery['group_by_key'];
}

try
{
// determine query type
if (strpos($query, 'insert') === false)
{
$rows = array();
$rowCounter = 0;

foreach ($this->db->query($query) as $row)
{
$fields = array();
$recordKey = $rowCounter;

foreach ($row as $key => $value)
{
// remember this key if it matches the user specified object key
if (($object_key != null) && ($key == $object_key))
$recordKey = $value;

// perform user specified coersions
if ($coersions != null)
{
switch ($coersions[$key])
{
case 'integer':
$value += 0;
break;

case 'float':
$value += 0.0;
break;

case 'datetime':
$value = new date_object($value);
break;

case 'base64_decode':
$value = base64_decode($value);
break;

case 'hex_decode':
$value = $this->hex_decode($value);
}
}

$fields[$key] = $value;
}

// perform grouping if requested
if ($group_by_key == null)
$rows[$recordKey] = $fields;
else
{
$groupKey = $fields[$group_by_key];

if ($rows[$groupKey] == null)
$rows[$groupKey] = array();

$rows[$groupKey][] = $fields;
}

$rowCounter++;
}

var_dump($rows);
}
else
{
// return last insert ID
return ($this->db->lastInsertId() + 0);
}
}
catch (PDOException $error)
{
print "Error!: " . $error->getMessage() . "<br/>";
die();
}
}

And here's some sample output

array(6) {
["task_id"]=>
int(22)
[0]=>
string(2) "22"
["container_id"]=>
int(3784)
[1]=>
string(4) "3784"
["name"]=>
string(12) "108-6972.XTG"
[2]=>
string(24) "3130382D363937322E585447"
}

Note how the named fields differ from their corresponding index fields.

task_id=> int(22) while [0]=> string(2) "22"
container_id=> int(3784 while [1]= > string(4) "3784"
name=> string(12) "108-6972.XTG" while [2]=> string(24) "3130382D363937322E585447"

Why is that? Is there any way to fix this without coercing the same value twice? Once for the key accessor and once for the index accessor?

Thanks In advance

Ken
.



Relevant Pages

  • Re: Java compatibility issues (WAS: MF having issues?)
    ... // indexed by an array of characters. ... Associative arrays are typically found in scripting languages like ... but they're also found in some general-purpose ...
    (comp.lang.cobol)
  • Unexpected values in PHP array
    ... I think this is probably just a misunderstanding on my part, but I'm creating associative arrays with string keys from MySQL query results and when I put a value in the array, I get the expected key association along with an index key that has a different value. ... I know all associative arrays have both key and index accessors, ...
    (php.general)
  • RE: [PHP] Unexpected values in an associative array
    ... I think this is probably just a misunderstanding on my part, but I'm creating associative arrays with string keys from MySQL query results and when I put a value in the array, I get the expected key association along with an index key that has a different value. ... Once for the key accessor and once for the index accessor? ... Maybe that's where your duplicates are coming from although I don't see duplicates in your output. ...
    (php.general)
  • Re: Array to hold multiple file content
    ... Using php's associative arrays, you can map a key, which in your case ... The strvalue function is used to convert the float into a string, ... My question is how would I go about creating an array that would hold ...
    (comp.lang.php)
  • Re: mysql to html table php function or class
    ... //1st: MySQL Query ... string containing HTML code of table parameters ... array containing the titles of columns. ... If he's going to be doing website updates, ...
    (comp.lang.php)