Nick Covox
Nick Covox

Reputation: 3

PHP adding array onto array

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
        )

)

Upvotes: 0

Views: 58

Answers (2)

Joran Den Houting
Joran Den Houting

Reputation: 3163

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
            )

    )

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111869

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);

}

Upvotes: 0

Related Questions