Make the loop and variable variables into a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Make the loop and variable variables into a function

Post by lauthiamkok »

Hi,

How can I make the code below into a function?

Code: Select all

# split the string by string on boundaries formed by the string delimiter will split the value into an array like the example below,
# Array
# (
#     [0] => pg_cat_id=1
#     [1] => tmp_id=4
#     [2] => parent_id=2
# )
$array_parent = explode("&", $string);
//print_r($array_parent);

# now loop the array.
for($i = 0; $i < count($array_parent); $i++)
{	
	# split the array into smaller arrays with the string delimiter, like the example below,
	# 	Array
	#	(
	#		[0] => pg_cat_id
	#		[1] => 1
	#	)
	#	Array
	#	(
	#		[0] => tmp_id
	#		[1] => 4
	#	)
	#	Array
	#	(
	#		[0] => parent_id
	#		[1] => 2
	#	)
	$array_child = explode("=", $array_parent[$i]);
	//print_r($array_child);
	
	# loop each of the array.
	for($a = 0; $a < count($array_child); $a++)
	{	
		# get the first value in each array and store it in a variable.
		$v = $array_child[0];
		
		# make the variable variable (sometimes it is convenient to be able to have variable variable names. 
		# that is, a variable name which can be set and used dynamically. a variable variable takes the value 
		# of a variable and treats that as the name of a variable). 
		${$v} = $array_child[1];
	}
}
so that I can call the function whenever I need it, such as below,

Code: Select all

$string = 'pg_cat_id=1&tmp_id=4&parent_id=2';

echo stringToVarVars($string);

echo $tmp_id; // I will get 4 as the restult.

Many thanks,
Lau
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Make the loop and variable variables into a function

Post by AbraCadaver »

PHP has already done this for you:

Code: Select all

$string = 'pg_cat_id=1&tmp_id=4&parent_id=2';
parse_str($string);
echo $tmp_id;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Make the loop and variable variables into a function

Post by lauthiamkok »

AbraCadaver wrote:PHP has already done this for you:

Code: Select all

$string = 'pg_cat_id=1&tmp_id=4&parent_id=2';
parse_str($string);
echo $tmp_id;
thanks alot! love PHP!! :D
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Make the loop and variable variables into a function

Post by shawngoldw »

Academically speaking, if you wanted to create a function to do this, how would you? Wouldn't the variables be out of scope?


Shawn
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Make the loop and variable variables into a function

Post by AbraCadaver »

Some ways:

The function could return an associative array of 'var'=>'val':

Code: Select all

function stringToVarVars($string) {
   // build $vars array
   return $vars;
}
extract(stringToVarVars($string));
//or
$vars = stringToVarVars($string);
extract($vars);
Or you could pass a var by reference and modify it as an associative array in the function:

Code: Select all

function stringToVarVars($string, &$vars) {
   // build $vars array
}
stringToVarVars($string, $vars);
extract($vars);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Make the loop and variable variables into a function

Post by AbraCadaver »

So those two will work in global scope or inside another function. This one will only work in global scope:

Code: Select all

function stringToVarVars($string) {
	foreach(explode('&', $string) as $assignment) {
		list($var, $val) = explode('=', $assignment);
		$GLOBALS[$var] = $val;
	}
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Make the loop and variable variables into a function

Post by shawngoldw »

But those still wouldn't work quite like parse_str since you would have to call another function afterwards to get the same result. I think the $GLOBALS method is the closest, but still not quite the same

Shawn
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Make the loop and variable variables into a function

Post by AbraCadaver »

shawngoldw wrote:But those still wouldn't work quite like parse_str since you would have to call another function afterwards to get the same result. I think the $GLOBALS method is the closest, but still not quite the same

Shawn
Yep, that's why I use parse_str() :)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply