Click to See Complete Forum and Search --> : array filtering


ma3743
11-09-2007, 03:15 AM
hi
I have a string like this:
$mystr= "1|26 1|68 1|62 3|2647 3|3010 2|3390 2|3544 1|4201 ";

I used explode function to have an array of that string:

$mystr= "1|26 1|68 1|62 3|2647 3|3010 2|3390 2|3544 1|4201 ";

$myarray = explode(' ', trim($mystr));

now I have an array like this:
0=>1|26
1=>1|68
2=>1|62
3=>3|2647
4=>3|3010
5=>2|3390
6=>2|3544
7=>1|4201

now again I want to change this array to 3 smaller array (one for 1| one for 2| and one for 3|) someting like this:

array 1:
0=>26
1=>68
2=>62
3=>4201

array 2:
0=>3390
1=>3544

array 3:
0=>2647
1=>3010

how can I do that?
thanks in advance

bokeh
11-09-2007, 03:34 AM
$mystr= "1|26 1|68 1|62 3|2647 3|3010 2|3390 2|3544 1|4201 ";

$myArray = array();

if(preg_match_all('/\b(\d+)\|(\d+)\b/', $mystr, $matches))
{
foreach($matches[1] as $k => $v)
{
$myArray[$v][] = $matches[2][$k];
}
}

ksort($myArray);Output:array (
1 =>
array (
0 => '26',
1 => '68',
2 => '62',
3 => '4201',
),
2 =>
array (
0 => '3390',
1 => '3544',
),
3 =>
array (
0 => '2647',
1 => '3010',
),
)

ma3743
11-09-2007, 04:02 AM
thank you so much dear bokeh