1

I need a way of sorting a string I have in PHP, the string is formatted like the one below, but is much bigger.

{ 1, 3, 1, 2, }, { 2, 3, 2, 1, }, { 3, 3, 2, 2, }, { 1, 2, 3, 1, },

What I would need it to do is turn each set of numbers that is in the brackets into an array. So in this case there would be four arrays with four values in each array.

The first array would look like the following:

array1[0] == 1
array1[1] == 3
array1[2] == 1
array1[3] == 2

How would I manage to do this?

1
  • array size is fixed = 4? Commented Apr 9, 2009 at 18:12

4 Answers 4

20
$inbound = "{ 1, 3, 1, 2, }, { 2, 3, 2, 1, }, { 3, 3, 2, 2, }, { 1, 2, 3, 1, }";
$inbound = trim(preg_replace("/,\\s*}/i", "}", $inbound), " ,");
$inbound = str_replace("{", "[", $inbound);
$inbound = str_replace("}", "]", $inbound);

$array_of_arrays = json_decode('[' . $inbound . ']');
Sign up to request clarification or add additional context in comments.

7 Comments

str_replace(array("{", "}"), array("[", "]"), $inbound) would also have worked.
echoing $array_of_arrays shows nothing, am I missing something?
No, I missed the trailing commas on the numbers. That's causing json_decode to fail. Hold for a moment.
Couldn't you just do str_replace(", }", "}", $inbound); instead of using regex?
Sorry to be a pain but when I use print_r ($array_of_arrays); all I receive as output is Array()
|
4

I can't comment (<50) but for Sean Bright, the problem with blank echo is that the json_decode doesn't like the trailing "," inside the [ ]..

edit :

$inbound = str_replace( array( '{', ', }' ), array( '[', ']' ), $inbound );

fixes.

Comments

3

I think I would split by Close Brace, then split by comma. That'd be the easiest way to write the code (possibly anyway, depending on your point of view), but not the most efficient in terms of complexity.

The most efficient would just be to walk through the array, performing different actions when you find a comma or brace:

 arrayOfArrays = new Array()
 masterIndex = 0
 arrayOfArrays[masterIndex] = new Array()

 for char c in string
   if c == '}'
     masterIndex++
     arrayOfArrays[masterIndex] = new Array()
   else if c == ','
     append num to arrayOfArrays[masterIndex]
   else if c is whitespace
     noop
   else
     append c to num

1 Comment

@vartec: Looks like pseudo-code to meand wouldn't be too hard to decipher. This is how I'd normally do it with anything else, although Sean's idea is pretty neat.
2
$str = "{ 1, 3, 1, 2, }, { 2, 3, 2, 1, }, { 3, 3, 2, 2, }, { 1, 2, 3, 1, },";
$matches = array();
$nArrays = preg_match_all('/{(.*)}/U',$str, $matches);
for($i=1;$i<=$nArrays; $i++) {
  $aArray = array(); 
  $nNums = preg_match_all('/(\d+)/',$matches[$i],$aArray);
  ${'array'.$i} = array();
  for($j=0;$j<=$nNums; $j++) {
    ${'array'.$i}[$j] = $aArray[$j+1];
  }  
}

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.