2011-03-24 8 views
34

मैं सरणी को संभालने के लिए कक्षा बनाने की कोशिश कर रहा हूं लेकिन मुझे इसमें काम करने के लिए array_map() नहीं मिल रहा है।array_map कक्षाओं में काम नहीं कर रहा है

<?php 
//Create the test array 
$array = array(1,2,3,4,5,6,7,8,9,10); 
//create the test class 
class test { 
//variable to save array inside class 
public $classarray; 

//function to call array_map function with the given array 
public function adding($data) { 
    $this->classarray = array_map($this->dash(), $data); 
} 

// dash function to add a - to both sides of the number of the input array 
public function dash($item) { 
    $item2 = '-' . $item . '-'; 
    return $item2; 
} 

} 
// dumps start array 
var_dump($array); 
//adds line 
echo '<br />'; 
//creates class object 
$test = new test(); 
//classes function adding 
$test->adding($array); 
// should output the array with values -1-,-2-,-3-,-4-... 
var_dump($test->classarray); 

यह आउटपुट

array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }

Warning: Missing argument 1 for test::dash(), called in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 11 and defined in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 15

Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 11 NULL

क्या मैं गलत कर रहा हूँ या इस समारोह बस के अंदर कक्षाएं काम नहीं करता है?

+0

के संभावित डुप्लिकेट की तरह उपयोग कर सकते हैं [पासिंग वस्तु विधि array_map करने के लिए()] (http://stackoverflow.com/questions/4546614/passing-object-method-to- सरणी-मानचित्र) – Gordon

उत्तर

96

आप गलत तरीके से कॉलबैक के रूप में dash निर्दिष्ट कर रहे हैं।

यह काम नहीं करता:

$this->classarray = array_map($this->dash(), $data); 

यह करता है:

अलग अलग रूपों एक कॉलबैक here लग सकता है के बारे में
$this->classarray = array_map(array($this, 'dash'), $data); 

पढ़ें।

+0

त्वरित रीप्ले के लिए धन्यवाद। मुझे यह काम मिल गया और आपकी मदद के लिए धन्यवाद। मैं बस सोच रहा था कि क्या आपको कॉलबैक के बारे में अधिक लेख होना है और सही तरीके से कैसे निर्दिष्ट करना है? – Justin

+0

@ जस्टिन: यहां एक नज़र डालें: http://stackoverflow.com/questions/48947/how-do-i-implement-a-callback-in-php – Jon

+0

यहां तक ​​कि स्थिर रूप से array_map (@ [self, 'डैश ']) – bortunac

1

यह

$this->classarray = array_map(array($this, 'dash'), $data); 

अवश्य पढ़ें array -thing एक वस्तु दृष्टान्त विधि के लिए PHP callback है। नियमित कार्यों के लिए कॉलबैक को फ़ंक्शन नाम ('functionName') युक्त सरल तारों के रूप में परिभाषित किया जाता है, जबकि स्थिर विधि कॉल को array('ClassName, 'methodName') के रूप में परिभाषित किया जाता है या इस तरह की स्ट्रिंग के रूप में: 'ClassName::methodName' (यह PHP 5.2.3 के रूप में कार्य करता है)।

+0

आपके उत्तर के लिए धन्यवाद। मैं इस विषय के बारे में और अधिक लेख जानने के लिए बहुत उपयोगी था? एक बार फिर धन्यवाद, – Justin

2

array_map($this->dash(), $data) 0 तर्कों के साथ $this->dash() पर कॉल करता है और सरणी के प्रत्येक सदस्य पर लागू करने के लिए कॉलबैक फ़ंक्शन के रूप में वापसी मान का उपयोग करता है। आप इसके बजाय array_map(array($this,'dash'), $data) चाहते हैं।

17

हैलो आपको यह एक

// Static outside of class context 
array_map(array('ClassName', 'methodName'), $array); 

// Static inside class context 
array_map(array(__CLASS__, 'methodName'), $array); 

// Non-static outside of object context 
array_map(array($object, 'methodName'), $array); 

// Non-static inside of object context 
array_map(array($this, 'methodName'), $array); 
संबंधित मुद्दे