Jump to content

String To Multidimensional Arrays


ckrudelux
 Share

Recommended Posts

Using the explode(); I can make an array from a string but how do I make a multidimensional array?

// Example:$string = somefunction($txt);print_r($string);//Output:Array(	[cars] => Array		(			[0] => Volvo			[1] => Saab		)	[color] => Array		(			[0] => Red			[1] => Green		))

Link to comment
Share on other sites

You'd probably have to write your own function that accepts a string. Explode that string into an array. Loop through the array and set each of the values equal to an exploded version of that value.
Not that I was looking for but I can do a function for it too. Just takes more time.
Link to comment
Share on other sites

How is the original string generated? If it can be constructed in JSON format, for instance, then unpacking it into a PHP object is quite simple.
Okay I just made a function now but the string looks like this now:$string = "1,2,3,4:2,1,2,3:3,1,2,3";With my function I get this:
Array(	[0] => Array //Every row is an object with content id number		(			[0] => 1 //This number tells me what object to change the other numbers tells me the id of the content			[1] => 2			[2] => 3			[3] => 4		)	[1] => Array		(			[0] => 2			[1] => 1			[2] => 2			[3] => 3		)	[2] => Array		(			[0] => 3			[1] => 1			[2] => 2			[3] => 3		))

I get the string from a javascript. Adding the numbers in the order it reads them first number is allways the object id number and the other is content id number.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...