-3

この例の関数のような関数に配列を渡すことは何とか可能ですか:

send_mail($adress, $data)
{
    return $data[0];
}

?

では、入力変数は配列でしょうか? はいの場合、誰かが私に例を示してもらえますか?

4

4 に答える 4

1

関数に配列を渡し、関数から配列を返すことができます。デフォルトでは、配列は(スカラー) で渡されます。つまり、配列のコピーであり、関数内の変更は元の配列には影響しません。

最初の関数呼び出しに続くコードでは、tempArray の内容には影響しません。アンパサンド (&) を使用して参照によって渡される 2 番目の関数呼び出しでは、元の配列が変更されます。

$tempArray = array();
$tempArray[] = "hello";

function testFn($theArray) {
    $theArray[] = "there";
    return $theArray;
}

$result = testFn($tempArray);
echo print_r($result);    // hello, there
echo print_r($tempArray);    // hello

function testFnRef(&$theArray) {
    $theArray[] = "there";
    return $theArray;
}

$result = testFnRef($tempArray);
echo print_r($result);    // hello, there
echo print_r($tempArray);    // hello, there

関数の引数

于 2013-07-07T17:46:39.390 に答える