A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearProcessing a string in PHP - page 1
User InfoPosts
Processing a string in PHP#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?

posted date: 2009-04-09 10:05:00


Re: Processing a string in PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-09 10:05:01


Re: Processing a string in PHP#3
array size is fixed = 4?

posted date: 2009-04-09 10:12:00


Re: Processing a string in PHP#4
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

posted date: 2009-04-09 10:13:00


Re: Processing a string in PHP#5
$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 . (]();

posted date: 2009-04-09 10:14:00


Re: Processing a string in PHP#6
$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]; } }

posted date: 2009-04-09 10:17:00


Re: Processing a string in PHP#7
question is about PHP.

posted date: 2009-04-09 10:18:00


Re: Processing a string in PHP#8
+1 for the creative use of json_decode :)

posted date: 2009-04-09 10:19:00


Re: Processing a string in PHP#9
Bright idea, I liked it too :) +1

posted date: 2009-04-09 10:21:00


Re: Processing a string in PHP#10
Interesting technique

posted date: 2009-04-09 10:22:00


Re: Processing a string in PHP#11
@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.

posted date: 2009-04-09 10:23:00


Re: Processing a string in PHP#12
str_replace(array("{", "}"), array("[", "]"), $inbound) would also have worked.

posted date: 2009-04-09 10:25:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.