PHP - 两个数组值为一

时间:2017-11-20 10:18:54

标签: php arrays multidimensional-array

我需要将两个多维数组合并到一个数组中,如下所示。

Booth数组每次都有相同的键(0,1,2,...)

第一个数组

url1 url2 每次都相同

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
        )

)

第二个数组

每个数组的键每次都会更改( 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
        )

)

最终结果

最终的数组应该相似:

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

)

代码

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

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

    }
}

响应仅从第二个数组的最后一个键添加值

    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 个答案:

答案 0 :(得分:1)

我只是做一个循环并创建字符串以推入合并数组:

对于这个功能感到抱歉,我只是颠倒了你打印出的阵列,所以我可以准确地使用你拥有它的数据。

信息不足:您的网址自相矛盾,这不是最好的主意!

网址应该更像这样:

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

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

我已根据您在问题中提出的要求做了我的回答,如果您根据链接的外观如何进行,请告诉我。

测试链接: 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>";

答案 1 :(得分:0)

我终于实现了我需要的代码:

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

}