tags:

views:

152

answers:

4

Hi, 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?

+3  A: 

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
Tom Ritter
question is about PHP.
vartec
@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.
Ross
+20  A: 
$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 . ']');
Sean Bright
+1 for the creative use of json_decode :)
Eric Petroelje
Bright idea, I liked it too :) +1
Thinker
Interesting technique
Ross
str_replace(array("{", "}"), array("[", "]"), $inbound) would also have worked.
Jasper Bekkers
echoing $array_of_arrays shows nothing, am I missing something?
Ryan
No, I missed the trailing commas on the numbers. That's causing json_decode to fail. Hold for a moment.
Sean Bright
There we go. Tested here and working.
Sean Bright
What is the regex replacing?
Pim Jager
Clever and concise. Nice
jerebear
Couldn't you just do str_replace(", }", "}", $inbound); instead of using regex?
ryeguy
Sorry to be a pain but when I use print_r ($array_of_arrays); all I receive as output is Array()
Ryan
Oh forget that, I have it working now, thanks Sean
Ryan
@ryeguy - I'm assuming that there might be some variance in the source data so I take zero-or-more spaces into account. If the source is guaranteed to always have a comma-space-brace then you can get away with a straight str_replace, yes.
Sean Bright
@Pim - trailing commas on the list of numbers inside the braces (i.e. "4, }")
Sean Bright
+2  A: 
$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];
  }  
}
vartec
+4  A: 

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.

Majin Magu
You are correct. I've addressed this in recent edits.
Sean Bright