If you read my last post about the htmlTagBuilder function, I used a technique of building part of the html tags from a key val array. This is a very useful technique and something else that I had mentioned was how this could be simplified by moving that repetitive code into a function. So, in this post, I’m going to write that function because it’s really quite simple and useful.
So, let’s get straight to it, here is our function definition.
function get_string_from_array($array, $keyValSeperator='=', $wordSeperator=' ')
Parameters are pretty straight forward, but let’s clarify just to be sure.
@array: The array of key val pairs to create the string out of. This is required.
@keyValSeperator: What to put in between each key value pair. For example, our htmlTagBuilder would always use ‘=’ so you get ‘key=val’. However, for certain queries you may want to use ‘!=’ instead.
@wordSeperator: What to put in between each set of key val pairs. For htmlTags it would just be spaces, but it could also be commas, or the word ‘AND’, or ‘OR’, etc.
Time to look at the actual function though which is really quite simple. The main part of the function is just going to go through the passed in function and change them from key val pairs, to strings using the $keyValSeperator which we are going to store in a new array and you’ll see why in the next step.
//create the new array
$newArray = array();
//now go through the passed in area and create the strings
foreach($array as $key => $val)
{
$newArray[] = $key.$keyValSeperator.'"'.$val.'"';
}
I think that’s pretty straight forward. Stick the passed in $keyValSeperator in between the keys and values. The one thing you’ll notice I tack on is quotes because of the fact that most everytime I use this function I’m creating an html tag, or a query string where quotes are necessary. However, this could be an additional parameter option if so you chose.
The next part of the array utilizes the $wordSeperator and the implode function. If you don’t know what implode does, read about it here
//now return the string created from implode
return implode($wordSeperator, $newArray);
That’s it really! If it doesn’t make sense feel free to ask some questions. I suppose an example or two wouldn’t hurt though.
//function call
get_string_from_array(array('type'=>'text', 'name'=>'username'));
//returns:
//type="text" name="username"
//function call
get_string_from_array(array('username'=>'awesomeghost', 'password'=>'p4ssw0rd'), '=', ' AND ');
//returns:
//username='awesomeghost' AND password='p4ssw0rd'
Well that’s it! I swear there are tons and tons of uses for this function. It further shows the usefulness of PHP’s wonderful key val arrays and useful functions like implode. Here’s the entire function all put together just so it’s all in one place:
function get_string_from_array($array, $keyValSeperator='=', $wordSeperator=' ')
{
//create the new array
$newArray = array();
//now go through the passed in area and create the strings
foreach($array as $key => $val)
{
$newArray[] = $key.$keyValSeperator.'"'.$val.'"';
}
//now return the string created from implode
return implode($wordSeperator, $newArray);
}
