English 中文(简体)
将两个或多个数组合并为相同的密钥数字
原标题:Merging two or more arrays by identical key numbers
  • 时间:2012-05-26 15:02:50
  •  标签:
  • php
  • arrays

我有3个阵列:

$array1 = array (1 =>  Hello  , 2 =>  nice  3 =>  how );
$array2 = array (1 =>  there  , 2 =>  to  3 =>  are );
$array3 = array (1 =>  champ  , 2 =>  meet  3 =>  you );

and i want to output a string that combines let s say from all the first keys of each array. e.g:

 echo "Hello there champ"
 echo "nice to meet"

How can i do that? The number of values in those arrays will always be identical since these are pulled from a table.

提前感谢。

问题回答
echo $array1[1] .     . $array2[1] .     . $array3[1]; // hello there champ 
echo $array1[2] .     . $array2[2] .     . $array3[2]; // nice to meet 

像这样吗?

function merge() {
    $arrays = func_get_args();
    $count = count($arrays[0]);

    for ($i = 0; $i < $count; $i++) {
        foreach ($arrays as $array) {
            echo $array[$i];
        }

        echo "<br />";
    }
}

$array1 = array (1 =>  helo  , 2 =>  nice  3 =>  how );
$array2 = array (1 =>  there  , 2 =>  to  3 =>  are );
$array3 = array (1 =>  champ  , 2 =>  meet  3 =>  you );

merge($array1, $array2, $array3);

产出:

hello there champ<br />nice to meet<br />how are you<br />




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签