TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-12-2008, 06:46 PM   #1 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default Array mess

Arrays are one thing I am trying to strengthen my skills on and have come upon a task that has stumped me for a bit now.

I have a database field called parameters, which contains data that looks like...

this=that,what=where,how=when

I have built a function to seperate the entire string as an array, getting the keys of this, what, and how. It also grabs the keys, that, where, and when.

I have a class to do all this, and class vars to contain the keys, to contain the original array, and to contain the values. What I am trying to do and what is stumping me is how to grab the value of a specific key.

The constructor process and parses the string into an array by calling the parse($string) method. Then I have another method called getValue($key), which is supposed to get a value of the array by the key.

This is all kinda hard to explain, but let me just show you some sloppy code ;)

Code:
 /**@var array of parameters */
 var $_params = null;

 /** @var keys of array */
 var $_keys = null;

 /** @var values of array */
 var $_values = null;
 
 /**
 * Constructor
 * Parses and grabs keys and values from database parameter set
 *
 * @param string Row from table to parse 
 */
 public function __construct($string)
 {
 	 $this->_params = $this->parse($string);
 }
 	
 /**
 * Parses string to seperate values and keys from comma seperated spacers
 *
 * @param string String to parse
 */
 protected function parse($string)
 {		
	// We need to split the string on , to get what=what, then slit that on = to get value
	$split = explode(',', $string);
		
	// Set a few array elements for containers of seperated data
	$sep  = array();
		
	// Cycle through split string, setting values and keys
	$i = 0; // For incrementing
	foreach($split AS $splited)
	{
		$sep[] = explode('=',$splited);
 		$this->_values[] = $sep[$i][1];
 		$this->_keys[] = $sep[$i][0];
 		++$i;
 	}
 }
 	
 /**
 * Gets param value by key
 *
 * @param string key name
 */
 public function getValue($key)
 {
 	 $key = trim($key);
 	 // Check if array key exists
 	 if(array_key_exists($key, $this->_params))
 	 {
              // Ive tried numerous things here, but cant seem to get it...
         }
 	 	 	
 }
Can anyone more familiar with arrays offer a bit of assistance, or point me to what array function I should be checking out? I have downloaded the manual ( that is such a handy thing to have around ) and have been searching through the array functions but haven't seen anything yet to do this easily.
Killswitch is offline  
Reply With Quote
Old 12-12-2008, 07:12 PM   #2 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default

Edit, I've modified the code a bit since posting, but for now I've tried array_flip to search the array based on the key ( which after flipped should be the value ) to return the new key ( old val ), but still no luck. I've var_dumped the array and the flipped array to see that the key was there, and sure enough it is and things flipped properly.

- Edit Though I edited my first post, sorry about that. Anyways, I figured out a way. I had a few issues when splitting the original string with whitespace which was throwing off checking for a match. Here's the final code ( not cleaned up )...

Code:
class parameters {
 
 	/**@var array of parameters */
 	var $_params = array();
 	/** @var keys of array */
 	var $_keys = null;
 	/** @var values of array */
 	var $_values = null;
 
 	/**
 	* Constructor
 	* Parses and grabs keys and values from database parameter set
 	*
 	* @param string Row from table to parse ... ex this=that, that=this, what=where
 	*/
 	public function __construct($string)
 	{
 	 	$this->parse($string);
	}
 	
 	/**
 	* Parses string to seperate values and keys from comma seperated spacers
 	*
 	* @param string String to parse
 	*/
 	protected function parse($string)
 	{		
		// We need to split the string on , to get what=what, then slit that on = to get value
		$split = explode(',', $string);
		$sep = array();
		
		// Cyce through split string, setting values and keys
		$i = 0; // For incrementing
		foreach($split AS $splited)
		{
		 	$sep[]   = explode('=',$splited);
 			$this->_values[]   = $sep[$i][1];
 			$this->_keys[]     = $sep[$i][0];
 			$this->_params[$sep[$i][0]] = $sep[$i][1];
 			++$i;
 		}
 	}
 	
 	/**
 	* Gets param value by key
 	*
 	* @param string key name
 	*/
 	public function getValue($key)
 	{ 	 
	  	$arr = $this->_params;	

 	 	foreach($arr AS $k => $v)
		{
		 	if($k == $key)
		   	{
		    		return $v;
			} 	
		}
	}

}

$string = 'something=test,this=that';
$params = new parameters($string);
$that = $params->getValue('this');
var_dump( $that );
This simply allows me to get a row of parameters from the database which are comma seperated as explained above. I can get specific values by using getValue(key name);. This is going to be used in a CMS where I have a plugin system with certain values that are set on the admin side of things.

Last edited by Killswitch : 12-12-2008 at 07:57 PM.
Killswitch is offline  
Reply With Quote
Old 12-12-2008, 07:15 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

You could do this: $this->_params[$key] in getValue.
Like so:
PHP Code:
public function getValue($key)
 {
      
$key trim($key);
      
// Check if array key exists
      
if(array_key_exists($key$this->_params))
      {
              return 
$this->_param[$key]; // -- This will return the value from that specific key
         
}
               
 } 
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-13-2008, 01:00 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

If you're willing and/or able to use some of the SPL features then you code could be whittled down to something like the following (just an example off the top of my head).

PHP Code:
class Parameters extends ArrayObject {
    
    public function 
__construct($string)
    {
        
$string str_replace(',''&'$string);
        
parse_str($string$data);
        
$this->exchangeArray($data);
    }
    
}

$string 'something=test,this=that';
$params = new Parameters($string);
// Can use array syntax, or offsetGet method
var_dump$params['this'] ); 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
KingOfTheSouth (12-13-2008)
Old 12-14-2008, 07:35 AM   #5 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default

Thanks. I've never worked with SPL before, gotta do some reading :D I really should have worked more with arrays other than doing the basics, but this was a good learning experience.
Killswitch is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I code this array benton General 3 10-22-2008 02:17 AM
Filling an array with an array benton General 5 08-05-2008 02:54 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
(array) or array()? Orc General 4 01-20-2008 07:52 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 11:54 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design