Php 如何将对象数组类定义作为方法执行USort()?
使用usort的原因是按名称按字母顺序重新排列顺序,但不知何故,它要么说明函数比较需要是一个数组,要么说明另一个错误,很明显我似乎已经通过了。任何帮助都将不胜感激,提前谢谢 之所以发生这种情况,是因为Php 如何将对象数组类定义作为方法执行USort()?,php,arrays,object,usort,Php,Arrays,Object,Usort,使用usort的原因是按名称按字母顺序重新排列顺序,但不知何故,它要么说明函数比较需要是一个数组,要么说明另一个错误,很明显我似乎已经通过了。任何帮助都将不胜感激,提前谢谢 之所以发生这种情况,是因为usort调用中的变量不是有效数组。您在任何地方都使用$this->\u contacts,但您的usort线路是: $all -> addContact($billy); // --> ended up adding each contact manually above $all
usort
调用中的变量不是有效数组。您在任何地方都使用$this->\u contacts
,但您的usort线路是:
$all -> addContact($billy);
// --> ended up adding each contact manually above
$all->Sort('name',true);
$items = $all->getItems();
foreach($items as $contact)
{
echo $contact->__toString();
}
$all->sort();
尝试将其更改为:
usort($this->contact, 'ContactList::' . $sortFct);
最终以这种方式解决了这个问题,希望它能帮助任何有同样问题的人。多谢太平绅士
usort($this->contact, 'ContactList::' . $sortFct);
usort($this->_contacts, 'ContactList::' . $sortFct);
<?php
class Contact{
public $name;
public $bgcolor;
public $lgcolor;
public $email;
public $element;
public function __construct($name, $bgcolor, $lgcolor, $email, $element)
{
$this->name = $name;
$this->bgcolor = $bgcolor;
$this->lgcolor = $lgcolor;
$this->email = $email;
$this->element = $element;
}
}
class ContactList implements Iterator, ArrayAccess
{
public $_label;
public $_contacts = array();
public function __construct($label)
{
$this->_label = $label;
}
public function getLabel()
{
return $this->_label;
}
public function addContact(Contact $contact)
{
$this->_contacts[] = $contact;
}
public function current()
{
return current($this->_contacts);
}
public function key()
{
return key($this->_contacts);
}
public function next()
{
return next($this->_contacts);
}
public function rewind()
{
return reset($this->_contacts);
}
public function valid()
{
return current($this->_contacts);
}
public function offsetGet($offset)
{
return $this->_contacts[$offset];
}
public function offsetSet($offset, $data)
{
if (!$data instanceof Contact)
throw new InvalidArgumentException('Only Contact objects allowed in a ContactList');
if ($offset == '')
{
$this->_contacts[] = $data;
} else
{
$this->_contacts[$offset] = $data;
}
}
public function offsetUnset($offset)
{
unset($this->_contacts[$offset]);
}
public function offsetExists($offset) {
return isset($this->_contacts[$offset]);
}
/* This is the comparing function to be used with usort to make it alphabetically ordered for All Contacts */
public function sort_by($field, &$arr, $sorting='SORT_DSC', $case_insensitive=true){
if(is_array($arr) && (count($arr)>0) && ( ( is_array($arr[0]) && isset($arr[0][$field]) ) || ( is_object($arr[0]) && isset($arr[0]->$field) ) ) ){
if($case_insensitive==true) $strcmp_fn = "strnatcasecmp";
else $strcmp_fn = "strnatcmp";
if($sorting=='SORT_DSC'){
$fn = create_function('$a,$b', '
if(is_object($a) && is_object($b)){
return '.$strcmp_fn.'($a->'.$field.', $b->'.$field.');
}else if(is_array($a) && is_array($b)){
return '.$strcmp_fn.'($a["'.$field.'"], $b["'.$field.'"]);
}else return 0;
');
}else if($sorting=='SORT_ASC'){
$fn = create_function('$a,$b', '
if(is_object($a) && is_object($b)){
return '.$strcmp_fn.'($b->'.$field.', $a->'.$field.');
}else if(is_array($a) && is_array($b)){
return '.$strcmp_fn.'($b["'.$field.'"], $a["'.$field.'"]);
}else return 0;
');
}
usort($arr, $fn);
return true;
}else{
return false;
}
}
}
?
$all->sort_by('name',$all->_contacts,'SORT_DSC','false');