/    Sign up×
Community /Pin to ProfileBookmark

array filtering

hi
I have a string like this:

[code=php]$mystr= “1|26 1|68 1|62 3|2647 3|3010 2|3390 2|3544 1|4201 “;[/code]

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

[code=php]
$mystr= “1|26 1|68 1|62 3|2647 3|3010 2|3390 2|3544 1|4201 “;

$myarray = explode(‘ ‘, trim($mystr));
[/code]

now I have an array like this:

[CODE]0=>1|26
1=>1|68
2=>1|62
3=>3|2647
4=>3|3010
5=>2|3390
6=>2|3544
7=>1|4201
[/CODE]

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:

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

array 2:
0=>3390
1=>3544

array 3:
0=>2647
1=>3010
[/CODE]

how can I do that?
thanks in advance

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 09.2007 — [code=php]$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);[/code]
Output:[CODE]array (
1 =>
array (
0 => '26',
1 => '68',
2 => '62',
3 => '4201',
),
2 =>
array (
0 => '3390',
1 => '3544',
),
3 =>
array (
0 => '2647',
1 => '3010',
),
)[/CODE]
Copy linkTweet thisAlerts:
@ma3743authorNov 09.2007 — thank you so much dear bokeh
×

Success!

Help @ma3743 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...