I'm trying to add onto an array while looping, although i'm not able to figure out how exactly to do this:
<?php
$original = array (
array ("title" => "one",
"color" => "blue"
),
array ("title" => "two",
"color" => "green"
)
);
$merged = array();
$str = "000three000red0!000four000white0!000five000black0!";
$pat = "/\d+(\D+)\d+(\D+)\d!/um";
preg_match($pat, $str, $match);
foreach($match as $result) {
$merged = array_merge($original,array("title" => $match[1], "color" => $match[2]));
print_r($merged);
}
The first problem is that is only seems to pick up the first match, the second being nothing ever gets added to $merged. I was hoping to have it output as:
Array
(
[0] => Array
(
[title] => one
[color] => blue
)
[1] => Array
(
[title] => two
[color] => green
)
[2] => Array
(
[title] => three
[color] => red
)
[3] => Array
(
[title] => four
[color] => white
)
[4] => Array
(
[title] => five
[color] => black
)
)
preg_match_all()
not justpreg_match()
.preg_match()
will stop after first occurance of your pattern.preg_match()
s behavior to do so.