Unexpected values in an associative array
- From: kentozier@xxxxxxxxxxx (Ken Tozier)
- Date: Tue, 31 Jul 2007 10:35:50 -0400
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
.
- Follow-Ups:
- RE: [PHP] Unexpected values in an associative array
- From: "Instruct ICC"
- Re: [PHP] Unexpected values in an associative array
- From: Jim Lucas
- Re: [PHP] Unexpected values in an associative array[Solved]
- From: Ken Tozier
- RE: [PHP] Unexpected values in an associative array
- Prev by Date: Re: [PHP] Objects
- Next by Date: Re: [PHP] Objects
- Previous by thread: Senior PHP Role
- Next by thread: Re: [PHP] Unexpected values in an associative array[Solved]
- Index(es):
Relevant Pages
|