Re: Unexpected values in an associative array[Solved]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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);
> > 						}
> > 					}
> > 					
> > 					$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
> >
> > -- 
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux