Page 1 of 1
Array
Posted: Thu Feb 24, 2011 2:50 pm
by jroxit
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.
Re: Array
Posted: Thu Feb 24, 2011 3:14 pm
by anantha
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
Re: Array
Posted: Thu Feb 24, 2011 3:29 pm
by AbraCadaver
How are you sending? Why not just use a standard method like post, get, send json encoded, etc...?
Re: Array
Posted: Thu Feb 24, 2011 3:59 pm
by jroxit
Abra,
This variable being sent is from a third-party process. They do use a "post" but they send that part of the post back as a delimited string.
Re: Array
Posted: Thu Feb 24, 2011 4:09 pm
by AbraCadaver
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
Re: Array
Posted: Thu Feb 24, 2011 4:27 pm
by jroxit
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.
Re: Array
Posted: Thu Feb 24, 2011 4:32 pm
by pickle
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.
Re: Array
Posted: Thu Feb 24, 2011 4:34 pm
by AbraCadaver
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.
Something like this may be easier and the vars will make more sense than explode():
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'];
Re: Array
Posted: Thu Feb 24, 2011 4:38 pm
by John Cartwright
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>';
should return something like
Array
(
[var1] => 123
[var2] => 234
[var3] => 345
[var4] => 456
)
Edit | Beaten to the punch
Re: Array
Posted: Thu Feb 24, 2011 4:47 pm
by pickle
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);
Re: Array
Posted: Thu Feb 24, 2011 6:43 pm
by jroxit
It's working now. Thank you all for your help. You all are awesome!!!
Re: Array
Posted: Thu Feb 24, 2011 7:02 pm
by jroxit
Ooops...one more question.
If the variable that I want to use is $var2, how would I insert spaces or periods between the numbers in the value.
for example: 234 would become 2 3 4 or 2.3.4
Thanks again in advance.
Re: Array
Posted: Thu Feb 24, 2011 9:36 pm
by McInfo
Re: Array
Posted: Thu Feb 24, 2011 9:51 pm
by jroxit
Thanks!!!
