本文介紹了可以將方法用作 array_map 函數嗎的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我想做這樣的事情:
<前>類 Cls {函數樂趣($php){return '西班牙的雨.';}}$ar = 數組(1,2,3);$instance = new Cls();print_r(array_map('$instance->fun', $ar));//^ 這行不通但 array_map 的第一個參數應該是函數的名稱.我想避免圍繞 $instance->fun 編寫包裝函數,但這似乎不可能.是真的嗎?
解決方案
是的,您可以對方法進行回調,如下所示:
array_map(array($instance, 'fun'), $ar)
查看 PHP 手冊中的回調類型了解更多信息
I want to do something like this:
class Cls { function fun($php) { return 'The rain in Spain.'; } } $ar = array(1,2,3); $instance = new Cls(); print_r(array_map('$instance->fun', $ar)); // ^ this won't work
but the first argument to array_map is supposed to be the name of the function. I want to avoid writing a wrapper function around $instance->fun, but it doesn't seem like that's possible. Is that true?
解決方案
Yes, you can have callbacks to methods, like this:
array_map(array($instance, 'fun'), $ar)
see the callback type in PHP's manual for more info
這篇關于可以將方法用作 array_map 函數嗎的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!