1

I have a string that looks like this:

$dash_access = "1-10:rw,14:rw|10-10:ro,14:ro";

My goal, is to break this string up into an array. I think I'm close, but can't quite figure it out. I want my array structure to look like this:

$array = Array  
               (
               [1] => Array
                   (
                   [10] => rw
                   [14] => rw
                   )
               [10] => Array
                   (
                   [10] => ro
                   [14] => ro
                   )
               )

This is what I have so far, but it's not working.

 $dash_access_split = explode("|",$dash_access); 
 for ($a=0;$a<count($dash_access_split);$a++) {
  $split1 = explode("-", $dash_access_split[$a]);  
  $split2 = explode(",", $split1[1]);              
  for ($b=0;$b<count($split2);$b++) {             
   $split3 = explode(":", $split2[$b]);            
   $dash_access_array[$split1[0]][] = $split3[0];
   $dash_access_array[$split1[0]][] = $split3[1]; 
  }   
 }
6
  • Some code would go well with the question. Commented Dec 13, 2013 at 4:11
  • @AshwinMukhija yeah my bad, got some code in there. Commented Dec 13, 2013 at 4:14
  • 1
    Will the string always contain the same chars? I see you're already using explode, but explode is hard on a dynamic string where the delimiter can change. Commented Dec 13, 2013 at 4:14
  • This question is very similar: stackoverflow.com/questions/20558940/… Commented Dec 13, 2013 at 4:16
  • @james no the string will not be the same length. But it will always be the same format. Commented Dec 13, 2013 at 4:18

2 Answers 2

3

Think of it as crumbling a cookie. Break it into progressively smaller pieces and process each piece accordingly.

Something like this should work

$dashAccess = "1-10:rw,14:rw|10-10:ro,14:ro";
$outArray = [];

foreach (explode('|', $dashAccess) as $bigPiece) {
    list($medKey, $medPiece) = explode('-', $bigPiece);
    $outArray[$medKey] = [];
    foreach (explode(',', $medPiece) as $smallPiece) {
        list($crumbleKey, $crumblePiece) = explode(':', $smallPiece);
        $outArray[$medKey][$crumbleKey] = $crumblePiece;
    }
}

var_dump($outArray);

Here's a fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Run the fiddle to see it in action. Also, if you are using PHP < 5.4, then use array() instead of []
0
<?php $dash_access = "1-10:rw,14:rw|10-10:ro,14:ro";
$big_array=explode('|',$dash_access);
$small_array=array();
foreach($big_array as $key=>$value)
{
    $small_array[]=explode('-',$value);
    foreach($small_array as $key => $value)
    {
        $chunk=explode(',',$value[1]);
        foreach($chunk as $value1)
        {
            $chunk_small=explode(':',$value1);
            $result[$value[0]][$chunk_small[0]]=$chunk_small[1];
        }
    }
}
print_r($result);

http://codepad.org/sKZyDu2m

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.