0

Just like following:

// situation1: pass whole array, and function use a part of this array.
function funcA1($array) {
    return $array['a']['a1'];
}

// situation2: pass $array['a'];
function funcA2($array) {
    return $array['a1'];
}


$a = ['a' => ['a1' => 1, 'a2' => 2], 'b' => 2, 'c' => 3];
$situation1 = funcA1($a);
$situation2 = funcA2($a['a']);

I think situation1 use more memory, but it's simple to use, and situation2 use less memory, but difficult to use. Is it right?

In the case of the array is very large and the funciton will be called for a lot of times, what solution should I choose, or other solution? And Why?

  • Depends on what you do with the data in the array - have a read of https://stackoverflow.com/questions/2030906/are-arrays-in-php-copied-as-value-or-as-reference-to-new-variables-and-when-pas. – Nigel Ren Feb 17 '21 at 14:55
  • I amend my question, add some detail of the function how to use the array. – stcnchenxin Feb 19 '21 at 01:50

0 Answers0