Re: [PHP] Unexpected values in an associative array[Solved]
Turns out that objects returned from SQL queries contain two parts for every field, one with a string key and one with an index key. Adding an is_numeric test on the keys allows you to filter out the numeric keys if you want to. For example: foreach ($row as $key = $value) { if (!is_numeric($key)) { /* do stuff here */ } } Ken On Jul 31, 2007, at 10:35 AM, Ken Tozier wrote: 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] =
Re: [PHP] Unexpected values in an associative array[Solved]
On 31/07/07, Ken Tozier [EMAIL PROTECTED] wrote: Turns out that objects returned from SQL queries contain two parts for every field, one with a string key and one with an index key. Adding an is_numeric test on the keys allows you to filter out the numeric keys if you want to. For example: Or don't get numeric keys in the first place: foreach ($this-db-query($query, PDO::FETCH_ASSOC) as $row) -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unexpected values in an associative array[Solved]
2007. 07. 31, kedd keltezéssel 11.29-kor Ken Tozier ezt írta: Turns out that objects returned from SQL queries contain two parts for every field, one with a string key and one with an index key. Adding an is_numeric test on the keys allows you to filter out the numeric keys if you want to. For example: thats the documented behaviour of mysql_fetch_array. but you do have mysql_fetch_assoc and mysql_fetch_row at hand, which return an array with one method of indexing only see: http://hu.php.net/mysql_fetch_array I don't know what your $this-db-query($query) uses, but you could look there... greets Zoltán Németh foreach ($row as $key = $value) { if (!is_numeric($key)) { /* do stuff here */ } } Ken On Jul 31, 2007, at 10:35 AM, Ken Tozier wrote: 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); } }
Re: [PHP] Unexpected values in an associative array[Solved]
On Jul 31, 2007, at 11:40 AM, Robin Vickery wrote: Or don't get numeric keys in the first place: foreach ($this-db-query($query, PDO::FETCH_ASSOC) as $row) Robin: Bingo! That did the trick. I knew my solution was hokey but I haven't used PDO before this project so wasn't aware of what it did behind the scenes. Thanks everyone for the quick replies. Ken -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unexpected values in an associative array[Solved]
On Tue, 2007-07-31 at 11:29 -0400, Ken Tozier wrote: Turns out that objects returned from SQL queries contain two parts for every field, one with a string key and one with an index key. Adding an is_numeric test on the keys allows you to filter out the numeric keys if you want to. For example: foreach ($row as $key = $value) { if (!is_numeric($key)) { /* do stuff here */ } } BAH, use the correct function. You're using mysql_fetch_array() when you SHOULD be using mysql_fetch_assoc(). Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php