Array

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
jroxit
Forum Newbie
Posts: 6
Joined: Thu Feb 24, 2011 2:40 pm

Array

Post 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.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Array

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array

Post by AbraCadaver »

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.
jroxit
Forum Newbie
Posts: 6
Joined: Thu Feb 24, 2011 2:40 pm

Re: Array

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array

Post 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
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.
jroxit
Forum Newbie
Posts: 6
Joined: Thu Feb 24, 2011 2:40 pm

Re: Array

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Array

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array

Post 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'];
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
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Array

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Array

Post 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);        
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jroxit
Forum Newbie
Posts: 6
Joined: Thu Feb 24, 2011 2:40 pm

Re: Array

Post by jroxit »

It's working now. Thank you all for your help. You all are awesome!!!
jroxit
Forum Newbie
Posts: 6
Joined: Thu Feb 24, 2011 2:40 pm

Re: Array

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Array

Post by McInfo »

Try str_split() followed by implode().
jroxit
Forum Newbie
Posts: 6
Joined: Thu Feb 24, 2011 2:40 pm

Re: Array

Post by jroxit »

Thanks!!! :D
Post Reply