将两个数组合并为一个字符串

时间:2022-10-10 12:19:20

I need to create a new array called ar1 with the items: [Dublin, Budapest, Copenhagen] and ar2 with [Ireland, Hungary, Denmark] after than answer with a string containing each country from the countries-array followed by the corresponding capital. Use the format "country = capital, * country = capital..."

我需要创建一个名为ar1的新数组,其中包含项目:[都柏林,布达佩斯,哥本哈根]和ar2与[爱尔兰,匈牙利,丹麦],然后用一个字符串回答,其中包含来自countries-array的每个国家/地区,后跟相应的首都。使用格式“country = capital,* country = capital ...”

Check code below but i know that is another way to doing that ex. For loop but can someone explain me how?

检查下面的代码,但我知道这是另一种方法来做到这一点。对于循环,但有人可以解释我怎么样?

$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$ANSWER = $ar2[0] . " = " . $ar1[0] . ", " . $ar2[1] . " = " . $ar1[1]. ", " . $ar2[2] . " = " . $ar1[2];

4 个解决方案

#1


2  

You should use a foreach and the key.

你应该使用foreach和密钥。

$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$ANSWER = '';
foreach($ar1 as $key => $capital) {
    $ANSWER .= $ar2[$key] . ' = ' . $capital . ', ';
}
echo rtrim($ANSWER, ', ');

... and then rtrim to remove the last ,.

...然后rtrim删除最后一个,。

https://3v4l.org/f8PJN

#2


1  

Another way to do it using array_combine()

使用array_combine()的另一种方法

<?php
$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$result = array_combine($ar2,$ar1);

$ANSWER = '';
$i = 0;
$comma = ', ';
$len = count($result);
foreach($result as $country => $capital) {
    if ($i == $len - 1){
        $comma='';
    }
    $ANSWER .= $country . ' = ' . $capital.$comma;
    $i++;
}

echo $ANSWER;

DEMO: https://3v4l.org/WGtJ3

Using array_map()

$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$input = array_combine($ar2,$ar1);
$output = implode(', ', array_map(
    function ($v, $k) { return sprintf("%s=%s", $k, $v); },
    $input,
    array_keys($input)
));

echo $output;

DEMO: https://3v4l.org/qps1G

#3


0  

More fast and simple way:

更快捷简单的方法:

$countries=["Ireland", "Hungary", "Denmark"];
 $capitals=["Dublin", "Budapest", "Copenhagen"];
 $string=implode(',',array_map(function($country,$capital){ return "$country=$capital";},$countries,$capitals));
 var_dump($string);

output:

string(50) "Ireland=Dublin,Hungary=Budapest,Denmark=Copenhagen"

#4


0  

If you've got two related lists in separate variables, it's often easier to transpose them into a single structure first. In PHP, you can do this like so:

如果在单独的变量中有两个相关列表,则首先将它们转换为单个结构通常更容易。在PHP中,您可以这样做:

$transposed = array_map(null, $ar1, $ar2);

Once they're combined, it's a lot more simple to generate the required output:

一旦它们组合在一起,生成所需的输出就会简单得多:

echo implode(', ', array_map(function($row) {
    return "{$row[1]} = {$row[0]}";
}, $transposed));

Ireland = Dublin, Hungary = Budapest, Denmark = Copenhagen

爱尔兰=都柏林,匈牙利=布达佩斯,丹麦=哥本哈根

See https://3v4l.org/LfvIY

#1


2  

You should use a foreach and the key.

你应该使用foreach和密钥。

$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$ANSWER = '';
foreach($ar1 as $key => $capital) {
    $ANSWER .= $ar2[$key] . ' = ' . $capital . ', ';
}
echo rtrim($ANSWER, ', ');

... and then rtrim to remove the last ,.

...然后rtrim删除最后一个,。

https://3v4l.org/f8PJN

#2


1  

Another way to do it using array_combine()

使用array_combine()的另一种方法

<?php
$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$result = array_combine($ar2,$ar1);

$ANSWER = '';
$i = 0;
$comma = ', ';
$len = count($result);
foreach($result as $country => $capital) {
    if ($i == $len - 1){
        $comma='';
    }
    $ANSWER .= $country . ' = ' . $capital.$comma;
    $i++;
}

echo $ANSWER;

DEMO: https://3v4l.org/WGtJ3

Using array_map()

$ar1 = ["Dublin", "Budapest", "Copenhagen"];
$ar2 = ["Ireland", "Hungary", "Denmark"];
$input = array_combine($ar2,$ar1);
$output = implode(', ', array_map(
    function ($v, $k) { return sprintf("%s=%s", $k, $v); },
    $input,
    array_keys($input)
));

echo $output;

DEMO: https://3v4l.org/qps1G

#3


0  

More fast and simple way:

更快捷简单的方法:

$countries=["Ireland", "Hungary", "Denmark"];
 $capitals=["Dublin", "Budapest", "Copenhagen"];
 $string=implode(',',array_map(function($country,$capital){ return "$country=$capital";},$countries,$capitals));
 var_dump($string);

output:

string(50) "Ireland=Dublin,Hungary=Budapest,Denmark=Copenhagen"

#4


0  

If you've got two related lists in separate variables, it's often easier to transpose them into a single structure first. In PHP, you can do this like so:

如果在单独的变量中有两个相关列表,则首先将它们转换为单个结构通常更容易。在PHP中,您可以这样做:

$transposed = array_map(null, $ar1, $ar2);

Once they're combined, it's a lot more simple to generate the required output:

一旦它们组合在一起,生成所需的输出就会简单得多:

echo implode(', ', array_map(function($row) {
    return "{$row[1]} = {$row[0]}";
}, $transposed));

Ireland = Dublin, Hungary = Budapest, Denmark = Copenhagen

爱尔兰=都柏林,匈牙利=布达佩斯,丹麦=哥本哈根

See https://3v4l.org/LfvIY