Jump to content

[SOLVED] Generate multilayered array from string.


kodie

Recommended Posts

ok so i have a string that looks like this:

blog:edit_all,delete_all|users:edit_all,delete_all|settings

 

and i want to make a code that can take that string and put it in a multi-layered array like this:

 

Array
(
    [0] => blog
        (
            [0] => edit_all
            [1] => delete_all
        )

    [1] => users
        (
            [0] => edit_all
            [1] => delete_all
        )

    [2] => settings
)

 

i've googled and everything and cant seem to get anything to work. ive come close but not quite.

 

any ideas?

thank you.

here it is:

$permissions1 = split("\|", $_SESSION['permissions']);
$i = "0";
	foreach ($permissions1 as &$perm) {
		$permissions2 = split(":", $perm);
		$permissions_a[$i] = $permissions2[0];
		$permissions_a[$i] = split(",", $permissions2[1]);
	$i++;
	}

 

and it outputs the array like this:

Array
(
    [0] => Array
        (
            [0] => edit_all
            [1] => delete_all
        )

    [1] => Array
        (
            [0] => edit_all
            [1] => delete_all
        )

    [2] => Array
        (
            [0] => 
        )

)

welp i ended up fixing her myself.

 

Here's the code that worked just incase anyone wanted to know:

$permissions1 = split("\|", $_SESSION['permissions']);
	foreach ($permissions1 as &$perm) {
		$permissions2 = split(":", $perm);
		if (isset($permissions2[1])) {
			$permissions[$permissions2[0]] = split(",", $permissions2[1]);
		} else { $permissions[$permissions2[0]] = Array("null"); }
	}

 

which gives me this array:

Array
(
    [blog] => Array
        (
            [0] => edit_all
            [1] => delete_all
        )

    [users] => Array
        (
            [0] => edit_all
            [1] => delete_all
        )

    [settings] => Array
        (
            [0] => null
        )

)

 

thanks anyways. :)

 

 

EDIT: if anyone cantell me how to mark as solved i will do that as well.

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.