0

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
        )

)
3
  • You need preg_match_all() not just preg_match(). preg_match() will stop after first occurance of your pattern. Commented Jul 11, 2014 at 12:37
  • @TiMESPLiNTER, i didn't know that. is there a reason it stops after the first match? Commented Jul 11, 2014 at 12:48
  • Yes beacause it's preg_match()s behavior to do so. Commented Jul 11, 2014 at 12:53

2 Answers 2

1

Full, including the preg_match_all:

$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_all($pat, $str, $match);

$merged = $original;
$i = 0;
foreach($match[1] as $result) {
    $merged[] = array("title" => $match[1][$i], "color" => $match[2][$i]);
    $i++;
}

print_r($merged);

results in:

    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
            )

    )
1
  • This seems to shove everything into groups by color, or title. I was needing to keep them in their own pairs. Thanks Commented Jul 11, 2014 at 12:45
0

The problem is:

foreach($match as $result) {

    $merged = array_merge($original,array("title" => $match[1], "color" => $match[2]));

    print_r($merged);

}

In each step of your loop you merge original array with new array and save the output into merged array so in fact you don't change original array and each time you set merged value again.

Change it into:

$merged = array(); // or $merged = $original; depending on your exact needs

foreach($match as $result) {

    $merged = array_merge($merged,array("title" => $match[1], "color" => $match[2]));

    print_r($merged);

}
1
  • This seems to forget about the $orginal array, and also doesn't keep the items in their own pair. thanks. Commented Jul 11, 2014 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.