简体   繁体   English

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

[英]Combine two arrays into a string

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. 我需要创建一个名为ar1的新数组,其中包含项目: [Dublin, Budapest, Copenhagen]和带有[爱尔兰,匈牙利,丹麦]的ar2 ,然后使用包含来自countries地区的每个国家/地区的字符串进行回答 - 后跟相应的资本。 Use the format "country = capital, * country = capital..." 使用格式"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];

You should use a foreach and the key . 你应该使用foreachkey

$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 https://3v4l.org/f8PJN

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 演示: https //3v4l.org/WGtJ3

Using array_map() 使用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 演示: https //3v4l.org/qps1G

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"

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 请参阅https://3v4l.org/LfvIY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM