English 中文(简体)
共同合并两个阵列
原标题:Trying to merge two arrays together

我有两阵列,我只是试图把我们合并成一个功能:

$var1=array();
$myInputVar=array();
$var1[ something ][ secondary_something ][]="foo1";
$var1[ something ][ secondary_something ][]="foo2";
$var1[ something ][ secondary_something ][]="foo3";

$myInputVar[ something ][ secondary_something ][]="foo4";
$myInputVar[ something ][ secondary_something ][]="foo5";

function something(&$array_, $array_new) {
   $array = array_merge($array_ , $array_new); 

    print_r($array);//for debugging

}   
something($var1, $myInputVar);

Now that prints:

Array ( [something] => Array ( [secondary_something] => Array ( [0] => foo1 [1] => foo2 [2] => foo3 ) ) )

我期待:

Array ( [something] => Array ( [secondary_something] => Array ( [0] => foo1 [1] => foo2 [2] => foo3 [3] => foo4 [4] => foo5) ) )

我还尝试:

$array = $array_ + $array_new;

那些仍然没有印刷我所期望的东西。

我感觉到,我误解了<代码>array_joint()功能的目的,这就是为什么它不退回结果I m期望它返回的原因。

任何想法?

最佳回答
问题回答

。 阁下:

$array_[ something ][ secondary_something ]

页: 1

$array_new[ something ][ secondary_something ]

你可以做以下工作:

function something(&$array_, $array_new) {
    $array = array_merge($array_ , $array_new); 
    return $array;
}

$var1[ something ][ secondary_something ] = something($var1[ something ][ secondary_something ], $myInputVar[ something ][ secondary_something ]);

否则,你就希望建立一种了解相关关键因素的职能,并将适当结合各阵列。

function something(&$array_, $array_new) {
    foreach ($array_new as $val)
        foreach ($val as $val2)
            foreach ($val2 as $val3)
                array_push($array_[ something ][ secondary_something ], $val3);
    print_r($array_);//for debugging
}  




相关问题
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 ...

热门标签