Reputation: 9570
I have a pattern
ab 23 cde 25 ... and so on
Can I capture the above pattern like
array(
[patterns] => array(
[0] => array(
[payer] => 'ab'
[amt] => 23
)
[1] => array(
[payer] => 'cde'
[amt] => 25
)
)
)
Is this possible?
Upvotes: 1
Views: 360
Reputation: 12814
Form a regex to match each item in your array (\w+\s+\d+
). Collect all of them (this should give you chunks like ab 23
).
Then for each of these items, split on whitespace and cram into your larger array as [payer], [amt].
Upvotes: 0
Reputation: 455052
You can use preg_match_all
for this as:
$str = 'ab 23 cde 25';
preg_match_all('/(\w+)\s+(\d+)/',$str,$matches);
$result = array();
for($i=0;$i<count($matches[0]);$i++) {
$result[] = array('payer' => $matches[1][$i], 'amt' => $matches[2][$i]);
}
Upvotes: 2