-1

I need to combine two multidimensional arrays to one array as the example provided below.

Booth arrays have same keys each time (0,1,2, ...)

The first array

The url1 or url2 are the same each time

Array
(
    [0] => Array
        (
            [Id] => 1
            [Title] => Example
            [Url1] => https://example1.com
            [Url2] => https://example2.com
        )

    [1] => Array
        (
            [Id] => 2
            [Title] => Example
            [Url1] => https://example1.com
            [Url2] => https://example2.com
        )

)

Second array

The keys of each array change every time (AL_url, MK_url, EN_url, SR_url, ...)

Array
(
    [0] => Array
        (
            [AL_url] => ?api=123?label=al
            [MK_url] => ?api=456?label=mk
        )

    [1] => Array
        (
            [EN_url] => ?api=789?label=en
        )

)

Final result

The final array should lookalike:

[Url1] + [AL_url] + [MK_url] = https://example1.com?api=123?label=al?api=456?label=mk

Array
(
    [0] => Array
        (
            [complete_url_1] => https://example1.com?api=123?label=al?api=456?label=mk
            [complete_url_2] => https://example2.com?api=123?label=al?api=456?label=mk
        )

    [1] => Array
        (
            [complete_url_1] => https://example1.com?api=789?label=en
            [complete_url_2] => https://example2.com?api=789?label=en
        )

)

The code

for ($y = 0; $y < count($second); $y++) {

foreach ($second[$y] as $key => $value) {
$new[$y]['complete_url_1'] = $first[$y]['Url1'] . $second[$y][$key];

    }
}

The response add the value only from the last key from second array

    Array
    (
        [0] => Array
            (
                [complete_url_1] => https://example2.com?api=456?label=mk
**this should be  https://example1.com?api=123?label=al?api=456?label=mk**
            )

        [1] => Array
            (
                [complete_url_1] => https://example1.com?api=789?label=en
            )

    )

2 Answers2

1

I would just do a loop and create the strings to push in to the merged array:

Sorry about the function, I just reversed your printed out arrays, so i could use the data exactly how you have it.

Little information: Your URLs are contradicting themselves, this isn't the greatest idea!

The URLs should look something more like this:

https://example1.com?api=123?label=al

https://example1.com?api=456?label=mk

I've done my answer based on what you've requested in your question, if you'd like it based on how the links should look then please let me know.

Test link: http://sandbox.onlinephpfunctions.com/code/70537b81816d95c3ddda12c1b68ba5cb072505c5

<?php

$array1 = "Array
(
    [0] => Array
        (
            [Id] => 1
            [Title] => Example
            [Url1] => https://example1.com
            [Url2] => https://example2.com
        )

    [1] => Array
        (
            [Id] => 2
            [Title] => Example
            [Url1] => https://example1.com
            [Url2] => https://example2.com
        )

)";

$array2 = "Array
(
    [0] => Array
        (
            [AL_url] => ?api=123?label=al
            [MK_url] => ?api=456?label=mk
        )

    [1] => Array
        (
            [EN_url] => ?api=789?label=en
        )

)";

function print_r_reverse($in) {
    $lines = explode("\n", trim($in));
    if (trim($lines[0]) != 'Array') {
        // bottomed out to something that isn't an array
        return $in;
    } else {
        // this is an array, lets parse it
        if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
            // this is a tested array/recursive call to this function
            // take a set of spaces off the beginning
            $spaces = $match[1];
            $spaces_length = strlen($spaces);
            $lines_total = count($lines);
            for ($i = 0; $i < $lines_total; $i++) {
                if (substr($lines[$i], 0, $spaces_length) == $spaces) {
                    $lines[$i] = substr($lines[$i], $spaces_length);
                }
            }
        }
        array_shift($lines); // Array
        array_shift($lines); // (
        array_pop($lines); // )
        $in = implode("\n", $lines);
        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        $pos = array();
        $previous_key = '';
        $in_length = strlen($in);
        // store the following in $pos:
        // array with key = key of the parsed array's item
        // value = array(start position in $in, $end position in $in)
        foreach ($matches as $match) {
            $key = $match[1][0];
            $start = $match[0][1] + strlen($match[0][0]);
            $pos[$key] = array($start, $in_length);
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
            $previous_key = $key;
        }
        $ret = array();
        foreach ($pos as $key => $where) {
            // recursively see if the parsed out value is an array too
            $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
        }
        return $ret;
    }
} 

$array1 = print_r_reverse($array1);
$array2 = print_r_reverse($array2);

$newArray = array();
for ($i = 0; $i < count($array1); $i++) {
    if(isset($array2[$i]["AL_url"]) && isset($array2[$i]["MK_url"])){ 
        $newArray[$i][] = $array1[$i]["Url1"] . $array2[$i]["AL_url"] . $array2[$i]["MK_url"];
        $newArray[$i][] = $array1[$i]["Url2"] . $array2[$i]["AL_url"] . $array2[$i]["MK_url"];
    }

    if(isset($array2[$i]["EN_url"])){ 
        $newArray[$i][] = $array1[$i]["Url1"] . $array2[$i]["EN_url"];
        $newArray[$i][] = $array1[$i]["Url2"] . $array2[$i]["EN_url"];
    }
}

echo "<pre>";
print_r($newArray);
echo "</pre>";
YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • 1
    Upvote for producing two separate URLs that *do not* contradict themselves. – JustCarty Nov 20 '17 at 11:08
  • 1
    Thank you for the code , it does what i wanted but it throws an error if the "EN_url" doesn't exist or any key from the second array. – user3255412 Nov 20 '17 at 11:11
  • @user3255412 Please check my test link, it won't throw an error if the "EN_url" doesn't exist as I've got a check with the if statement. – YaBCK Nov 20 '17 at 11:38
  • Yes in this case it doesn't throw an error but as i mentioned on the question the keys on the second array change as i request the array. – user3255412 Nov 20 '17 at 13:38
  • @user3255412 Could you show me a snippet of the code which generates your arrays? As I can adapt my code to work for your instance – YaBCK Nov 20 '17 at 14:17
0

I finally achieved what i needed here is the code:

for ($y = 0; $y < count($second); $y++) {
    $final[$y]['complete_url_1'] = $first[$y]['complete_url_1']. implode('',$second[$y]);
    $final[$y]['complete_url_2'] = $first[$y]['complete_url_1']. implode('',$second[$y]);

}