Php 向函数传递和返回数组

Php 向函数传递和返回数组,php,Php,我已经创建了两个数组。我想把这两个数组传递给任何函数。我是一个函数初学者,所以尝试用粗糙的代码来完成我的任务。因为我在$cntctnum和$CNTCTYPE命名数组中有一些值 $cntctnum = array(); $cntcttype = array(); $response = array(); function play_with_array($cntctnum, $cntcttype){ $contactnumber= $cntctnum[]; $cnt

我已经创建了两个数组。我想把这两个数组传递给任何函数。我是一个函数初学者,所以尝试用粗糙的代码来完成我的任务。因为我在$cntctnum和$CNTCTYPE命名数组中有一些值

 $cntctnum      = array();
 $cntcttype     = array();
 $response = array();

 function play_with_array($cntctnum, $cntcttype){
 $contactnumber= $cntctnum[];
 $cntcttype = $cntcttype[];

 // some code to play with array.

 return resultarray();
 }


 $response = play_with_array($cntctnum, $cntcttype);
这是在数组中传递函数的正确方法吗?
我需要在RealTrayLaye()返回之前或在返回RealTayl()时声明为$数组,它会自动将其视为数组?< /p> < P>。您不必预先定义<代码> $Rebug <代码>,但不管怎样,根据代码后来的期望,这样做可能是个好主意。 在函数中,您不会返回函数调用
resultarray()
,而是返回一个新数组:

function play_with_array($cntctnum, $cntcttype) {
    $contactNumber = $cntctnum; // You don't need the assignment! Note: No brackets here!
    $resultArray = array();
    // Do something here        
    return $resultArray;
}
你有:

$cntctnum      = array();
$cntcttype     = array();
$response = array();

function play_with_array($cntctnum, $cntcttype){
$contactnumber= $cntctnum[]; // don't need []
$cntcttype = $cntcttype[];   // don't need []

// some code to play with array.

return resultarray(); // this is a function?
}


$response = play_with_array($cntctnum, $cntcttype);
我会这样做

$cntctnum    = array();
$cntcttype    = array();

function play_with_array($contactnumber, $cntcttype){
    // some code to play with array.
    $response = array($contactnumber,$cntcttype);

    return $response; 
}

$response = play_with_array($cntctnum, $cntcttype);
echo "<pre>".print_r($response,true)."</pre>";
$cntctnum=array();
$cntcttype=array();
使用数组($contactnumber,$cntcttype)播放函数{
//一些代码可以使用数组。
$response=array($contactnumber,$cntctype);
返回$response;
}
$response=play_与_数组($cntcnum,$cntctype);
回显“.print_r($response,true)”;

Rahul,将数组传递给函数就像将任何其他变量传递给函数一样。拥有函数的原因是拥有一个代码块,该代码块可重复用于执行同一组计算等

很难理解您在函数中试图做什么。如果这是可重复的,意思是:比较数组1和数组2,得到较大的一个,并返回它的某种变体,然后是的,创建一个函数。如果函数中有一些代码只针对本页和这两个数组,则不需要该函数。只需内联编写代码

< P>我需要在RealTrayLaye()返回之前或之后声明为数组响应,它会自动将其视为数组?< /p>
不,你没有。如果函数中的resultarray变量真的是数组,那就很好了。

这些看起来像是拼写错误的证明变量名称我的目的不是纠正拼写错误。。。我已经创建了一个虚拟函数来粗略地理解我的需求。你能不能想出像$array\u test或$array\u one这样更简单的名字?在“CNTCTYPE”中,很容易错误地推销一封信并引起错误