我是php的新手。我试图使用下面的递归静态函数,但在我的代码中执行时,我收到错误:
PHP致命错误:在第36行的myutil.php中调用未定义的函数seekKey()static function seekKey($haystack, $needle)
{
$valuesArray=null;
foreach($haystack as $key => $value){
if($key === $needle){
$valuesArray[] = $value;
}elseif(is_array($value)){
self::seekKey($value, $needle);
}
}
$valuesArray = array_merge($valuesArray, seekKey($value, $needle));
return $valuesArray;
}
func createAddressBook() -> Bool {
if self.addressBook != nil {
return true
}
var err : Unmanaged<CFError>? = nil
let addressBook : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
if addressBook == nil {
println(err)
self.addressBook = nil
return false
}
self.addressBook = addressBook
getContactNames()
return true
}
func getContactNames() {
if !self.determineStatus() {
println("not authorized")
return
}
let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray as [ABRecord]
for person in people {
var contactName = ABRecordCopyCompositeName(person).takeRetainedValue() as String
self.contacts.append(contact(name: contactName))
}
}
请告诉我在上面的代码中需要更改的内容?
答案 0 :(得分:4)
假设seekKey()
是类中的静态方法,那么您需要self::seekKey($value, $needle);
但是当seekKey()
返回一个值时,您需要在调用它时对该返回值执行某些操作递归地......也许$valuesArray = array_merge($valuesArray, seekKey($value, $needle));
static function seekKey($haystack, $needle)
{
$valuesArray=[];
foreach($haystack as $key => $value){
if($key === $needle){
$valuesArray[] = $value;
}elseif(is_array($value)){
$valuesArray = array_merge($valuesArray, self::seekKey($value, $needle));
}
}
return $valuesArray;
}