0

我有两个变量,它们都是数组:

$var1=array();

$var1['something']['secondary_something'][1]="foo";
$var1['something']['secondary_something'][2]="foo";
$var1['something']['secondary_something'][3]="foo";
$var1['something']['secondary_something'][4]="foo";

现在我有一个函数,它需要一个数组作为输入:

function something($input=array()){

print_r($input);//print the array out

}//end of function

但我需要$input像对 的引用$var1,所以当我调用函数时,像这样附加到变量 2 ( $input):

$myInputVar=array();
$myInputVar['something']['secondary_something'][]="foo";
$myInputVar['something']['secondary_something'][]="foo";
//Notice how I append to the var above, not giving a key name in the third dimension of the array.
something($myInputVar);

现在这将简单地打印:

Array ( [something] => Array ( [secondary_something] => Array ( [0] => foo [1] => foo ) ) )

但我需要第二个 var ( $input,来自函数) 作为第一个 var ( $var1) 的引用。

所以,最终结果应该是:

Array ( [something] => Array ( [secondary_something] => Array ( [5] => foo [6] => foo ) ) )

有些人告诉我使用=&(这使得一个 var 引用另一个),但我似乎无法理解=&在这种情况下我将如何使用它。

我想要做的甚至可能吗?如果是这样,请你照亮它。

4

3 回答 3

0

我很难理解你想要达到的目标。为什么要打印数组?如果要将某些内容推入数组,则始终需要一个值和一个数组。
你可以这样做

function something(&$array, $val) { // take the assigned $array as reference
  $array[] = $val;
}

然后调用

$test = array();
something($test, 'foo');
print_r($test); // => array ( 'test' )

我希望这有帮助


编辑

function something(&$array, $array_new) {
   $array = array_merge($array, $array_new); // to combine/merge both arrays
   // please keep in mind that $array is technically $var, because it references to this variable
}

$var1=array();

$var1['something']['secondary_something'][1]="foo";
$var1['something']['secondary_something'][2]="foo";
$var1['something']['secondary_something'][3]="foo";
$var1['something']['secondary_something'][4]="foo";

$myInputVar=array();
$myInputVar['something']['secondary_something'][]="foo";
$myInputVar['something']['secondary_something'][]="foo";

something($var, $myInputVar);
于 2012-04-15T16:36:21.547 回答
0

您可以使函数接收对数组的引用。像这样:

function something(&$arr) {
    $arr['something']['secondary_something'][]="something1";
    $arr['something']['secondary_something'][]="something2";
}

现在您可以执行以下操作:

$var1 = array();
$var1['something']['secondary_something'][1]="foo";
$var1['something']['secondary_something'][2]="bar";
$var1['something']['secondary_something'][3]="foobar";
$var1['something']['secondary_something'][4]="barfoo";
something($var1); //this should now append the items to $var1.
print_r($var1); //check to see if calling 'something' really did add the items.
于 2012-04-15T16:39:31.337 回答
0

试试这个:

$var1 = array();
$var2 = array();

assign($var1, $var2, 'foo'); // add an item in both $var1 & $var2
assign($var1, $var2, 'bar'); // append another item it both $var1 & $var2
assign($var1, $var2, 'buz', 5); // add an item in both $var1 & $var2 at index 5

echo print_r($var1, TRUE) . print_r($var2, TRUE);

function assign(&$var1, &$var2, $value, $index=NULL) {
    if (is_int($index)) {
        $var1['something']['secondary_something'][$index] = $var2['something']['secondary_something'][$index] = $value;
    } else {
        $var1['something']['secondary_something'][] = $var2['something']['secondary_something'][] = $value;
    }
}

这将输出:

Array ( [something] => Array ( [secondary_something] => Array ( [0] => foo [1] => bar [5] => buz ) ) ) Array ( [something] => Array ( [secondary_something] => Array ( [0] => foo [1] => bar [5] => buz ) ) ) 
于 2012-04-15T17:00:52.780 回答