TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Array mess (http://www.talkphp.com/absolute-beginners/3760-array-mess.html)

Killswitch 12-12-2008 06:46 PM

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 12-12-2008 07:12 PM

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.

Orc 12-12-2008 07:15 PM

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


Salathe 12-13-2008 01:00 AM

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'] ); 


Killswitch 12-14-2008 07:35 AM

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.


All times are GMT. The time now is 01:59 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0