Jump to content

string to array


ultraviolet_998

Recommended Posts

Hi Everyone,

 

I've been really battling with this feature. I seem to have it right up until the part of rebuilding the array from the string.

 

Herewith is my array2string code:

 

function api_array2string($array)
{
$return = "";
$null_value = "";

foreach ($array as $key => $value) {
	if (is_array($value)) {
		$return_value = "(".api_array2string($value);
	} else {
		$return_value = $value."|";
	}
	$return .= $key."=".$return_value;
}
$return .= ")";
return $return;
}

 

Which converts the following array into the following string:

 

Array
(
    [wd] => Array
        (
            [0] => Array
                (
                    [0] => wd.co.za
                    [realm] => wd.co.za
                    [1] => SHAPED
                    [service] => SHAPED
                    [2] => Shaped ADSL
                    [description] => Shaped ADSL
                    [3] => wd
                    [userName] => wd
                )

            [1] => Array
                (
                    [0] => wdunshaped.co.za
                    [realm] => wdunshaped.co.za
                    [1] => UNSHAPED
                    [service] => UNSHAPED
                    [2] => Unshaped ADSL
                    [description] => Unshaped ADSL
                    [3] => wd
                    [userName] => wd
                )

        )

    [test] => Array
        (
            [0] => Array
                (
                    [0] => test.com
                    [realm] => test.com
                    [1] => SHAPED
                    [service] => SHAPED
                    [2] => Shaped ADSL
                    [description] => Shaped ADSL
                    [3] => test
                    [userName] => test
                )

            [1] => Array
                (
                    [0] => test-local.com
                    [realm] => test-local.com
                    [1] => LOCAL
                    [service] => LOCAL
                    [2] => Local-only ADSL
                    [description] => Local-only ADSL
                    [3] => test
                    [userName] => test
                )

        )

)

 

array(wd=array(0=array(0=wd.co.za,realm=wd.co.za,1=SHAPED,service=SHAPED,2=Shaped ADSL,description=Shaped ADSL,3=wd,UserName=wd);1=array(0=wdunshaped.co.za,realm=wdunshaped.co.za,1=UNSHAPED,service=UNSHAPED,2=Unshaped ADSL,description=Unshaped ADSL,3=wd,UserName=wd));test=array(0=array(0=test.com,realm=test.com,1=SHAPED,service=SHAPED,2=Shaped ADSL,description=Shaped ADSL,3=test,UserName=test);1=array(0=test-local.com,realm=test-local.com,1=LOCAL,service=LOCAL,2=Local-only ADSL,description=Local-only ADSL,3=test,UserName=test)))

 

Now I was using strpos to run through the string isolating the "(" and then cutting up the string as it went on, but it seems I can't get it to work.

 

Any suggestions on how to get this to work, or perhaps my string is retarded and can be simplified?

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/43172-string-to-array/
Share on other sites

Yes, that what the functions serialize() and unserialize() do. The serialize() function will convert an array to a string and the unserialize() function will recreate the array from the serialized string.

 

Read the manual references I posted.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/43172-string-to-array/#findComment-209630
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.