按引用返回一个值,而不是按值返回,这样就无需为变量建立一个重复的副本
1 function &array_find_value($needle, &$haystack) 2 { 3 foreach ($haystack as $key=>$value){ 4 if ($needle == $value){ 5 //必须返回一个变量的引用,而不能是包含这个变量的字符串 6 return $haystack[$key]; 7 } 8 } 9 } 10 $artists = ['table','The Doors']; 11 $band =& array_find_value('The Doors',$artists); 12 var_dump($band); // string 'The Doors' 13 //改变数组 14 $band = 'thhe doors name is panpan'; 15 var_dump($artists); 16 /* 17 array (size=2) 18 0 => string 'table' (length=5) 19 1 => &string 'thhe doors name is panpan' (length=25) 20 */