douguomou5094 2017-11-20 10:18
浏览 35

PHP - 两个数组值为一

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条回答 默认 最新

  • dougong1031 2017-11-20 10:43
    关注

    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("
    ", 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("
    ", $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>";
    
    评论

报告相同问题?