Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a dynamic array based on dynamically built string 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
Hello!

I have written or in process of writing a a javascript which
a) scans through any form
b) identifies each field
c) based on field type, extracts its name and value
d) names & values are paired into a variable
e) built variable is returned as a variable string

So, given (a) through (e), I can end up with a string that looks like

name::James Smith~phone::(555) 555-1212~Status::Member~Income::20000~

I need to pass this string to PHP, using AJAX, and then have PHP parse it, explode it or do whatever it needs to do to pair the names with their respective values in a form of an array.

I could create the string in javascript in any format if it helps PHP end of things ... I even though of using something like

[name=>James Smith,phone>=(555) 555-1212,Status=>Member,Income=>20000]

but when I passed this format to PHP, PHP saw it as a "single" string and doing

Code:
$array = array($above-built-string);

did not yield an array with multiple keys ...

Where am I going wrong?

Your assistance, as always, is greatly appreciated.

Regards,


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
It may not be the most efficient way of doing it, but assuming you have a string like the first one:

name::James Smith~phone::(555) 555-1212~Status::Member~Income::20000~

Code:
$thevar = "name::James Smith~phone::(555) 555-1212~Status::Member~Income::20000~";

$ex1 = explode("~",$thevar);

$newarr = array();

foreach($ex1 as $key=>$val)
{
		$pairs = explode("::",$val);
		$newarr[$pairs[0]] = $pairs[1];
}

As to why the array construction did not work, its because at the end its still a single string. PHP does not automatically interpret the string into an array. You could still parse it though to get a usable array. But your first string should work better.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Nice!

I just had to change

Code:
$newarr[$pairs[0]] = $pairs[1];

TO

if(isset($pairs[1])) { $newarr[$pairs[0]] = $pairs[1]; }

and got my dynamic array built from a dynamically built variable ... ;-)

Thanks!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Glad it worked for you.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top