Array
Moderator: General Moderators
Array
Hi,
I'm new to PHP. I have been an .asp programmer for a long time. I'm sending variables to a PHP page from an already built .asp page. The variable that's being sent is: passinginfo. This variable is composed of a bunch of other variables separated by the | symbol. For example:
passinginfo = (var1=123|var2=234|var3=345|var4=456)
I need to break these out and then set the values to new variables. This is what I need:
myvar1 = value of var1 (or myvar1=123)
myvar2 = value of var2 (or myvar2=234)
How would I do this in PHP?
I think it would be an explode function, but I'm not at all sure where to go after that. I'm looking for the most efficient/safe way to do this. Thanks in advance for any and all help.
I'm new to PHP. I have been an .asp programmer for a long time. I'm sending variables to a PHP page from an already built .asp page. The variable that's being sent is: passinginfo. This variable is composed of a bunch of other variables separated by the | symbol. For example:
passinginfo = (var1=123|var2=234|var3=345|var4=456)
I need to break these out and then set the values to new variables. This is what I need:
myvar1 = value of var1 (or myvar1=123)
myvar2 = value of var2 (or myvar2=234)
How would I do this in PHP?
I think it would be an explode function, but I'm not at all sure where to go after that. I'm looking for the most efficient/safe way to do this. Thanks in advance for any and all help.
Re: Array
you can do an explode...http://us.php.net/manual/en/function.explode.php .........the php site has a good manual...u can look over it
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Array
How are you sending? Why not just use a standard method like post, get, send json encoded, etc...?
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.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Array
So what is actually received as a string by PHP?
passinginfo = (var1=123|var2=234|var3=345|var4=456)
or
(var1=123|var2=234|var3=345|var4=456)
or
var1=123|var2=234|var3=345|var4=456
passinginfo = (var1=123|var2=234|var3=345|var4=456)
or
(var1=123|var2=234|var3=345|var4=456)
or
var1=123|var2=234|var3=345|var4=456
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.
Re: Array
Abra,
It's actually coming back as: passinginfo=var1=123|var2=234|var3=345|var4=456
I need to strip out the variable values and change them into variables to use in other places in my page. I might want to use the value of var2 (which is in this case, 234) as the number of players on a roster.
It's actually coming back as: passinginfo=var1=123|var2=234|var3=345|var4=456
I need to strip out the variable values and change them into variables to use in other places in my page. I might want to use the value of var2 (which is in this case, 234) as the number of players on a roster.
Re: Array
So ya - explode().
Just strip off "passinginfo=", then explode() on |. That'll give you an array of "var1=123", "var2=234", etc. You can then iterate through that array, explode()ing again on each item - this time on "=", which will give you the variable name and variable value.
Just strip off "passinginfo=", then explode() on |. That'll give you an array of "var1=123", "var2=234", etc. You can then iterate through that array, explode()ing again on each item - this time on "=", which will give you the variable name and variable value.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Array
Something like this may be easier and the vars will make more sense than explode():jroxit wrote:Abra,
It's actually coming back as: passinginfo=var1=123|var2=234|var3=345|var4=456
I need to strip out the variable values and change them into variables to use in other places in my page. I might want to use the value of var2 (which is in this case, 234) as the number of players on a roster.
Code: Select all
$string = 'passinginfo=var1=123|var2=234|var3=345|var4=456';
parse_str(str_replace('passinginfo=', '', str_replace('|', '&', $string)));
echo $var1;
//or
parse_str(str_replace('passinginfo=', '', str_replace('|', '&', $string)), $myvars);
echo $myvars['var1'];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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Array
Code: Select all
$input = 'passinginfo=var1=123|var2=234|var3=345|var4=456';
$input = str_replace('passinginfo=', '', $input);
$input = str_replace('|', '&', $input);
parse_str($input, $vars);
echo '<pre>'. print_r($vars, true) .'</pre>';Array
(
[var1] => 123
[var2] => 234
[var3] => 345
[var4] => 456
)
Edit | Beaten to the punch
Re: Array
Well hell, if we're doing it for him, golf it is:
Code: Select all
$input = 'passinginfo=var1=123|var2=234|var3=345|var4=456';
parse_str(str_replace(array('passinginfo=','|'),array('','&'), $input), $vars);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.