RE: [PHP] Unexpected values in an associative array



From: Ken Tozier <kentozier@xxxxxxxxxxx>
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.

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"
}

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

What is $coersions or $inQuery['coersions']? Maybe a var_dump or a print_r on that for the list?
What is $value and what is this supposed to do:
case 'integer':
$value += 0;
break;

case 'float':
$value += 0.0;

If $value is a string, you should quote your 0 and 0.0. What add 0 or 0.0 to an int or float respectively?

Do a print_r on $row before your foreach ($row as $key => $value) and echo your $key and $value as the first line in after that for loop. Then echo $coersions[$key]. Does $coersions[$key] have what you want for switch ($coersions[$key]) at this point? I assume you expect $coersions to contain datatypes, but does it?

Perhaps "start at the end" considering the structure of the output array you want, then consider how to populate it to have that structure.

What's the extra "[]" about in $rows[$groupKey][] = $fields;
Maybe that's where your duplicates are coming from although I don't see duplicates in your output.

_________________________________________________________________
http://liveearth.msn.com
.



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)
  • 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. ... I know all associative arrays have both key and index accessors, ...
    (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: Elementary but surprisingly difficult.
    ... the original message from Albert-- the one I first replied to-- described the problem as removing duplicates from an array. ... Now I hate to get all Computer Science on everyone, but the very definition of array denotes an ordering; you can say the "first" item in an array, and it has clear and unambiguously meaning. ... So no, if preserving the original order mattered, "bdeca" wouldn't work, because the original order had a "a" as the *first* element. ... When a duplicate item is found, the "read" pointer is advanced past the item and the write pointer is left where it is. ...
    (comp.lang.forth)