當然,一個對象會是簡單的方法嗎?
class Item {
public $unique_url;
public $url;
public $other_data;
public function __construct($unique_url, $url, $other_data)
{
$this->unique_url = $unique_url;
$this->url = $url;
$this->other_data = $other_data;
}
}
class ItemArray {
private $items = array();
public function __construct()
{
}
public function push(Item $item)
{
array_push($items, $item); //These may need to be reversed
}
public function getByURL($url)
{
foreach($items as $item)
{
if($item->url = $url)
{
return $item;
}
}
}
public function getByUniqueURL($url)
{
foreach($items as $item)
{
if($item->unique_url = $unique_url)
{
return $item;
}
}
}
}
然後用
$itemArray = new ItemArray();
$item = new Item("someURL", "someUniqueURL","some other crap");
$itemArray->push($item);
$retrievedItem = $itemArray->getItemByURL("someURL");
使用這種技術,由於對象創建一些額外的開銷,但除非你做行瘋狂的數字,將被罰款。
不是真的,沒有。只需製作兩個陣列。開銷很小(很好,取決於數組的大小)。 – 2010-12-10 03:12:24
你需要修改「隨機數據」,一旦它在數組中?而且它有可能造成unique_id和URL發生衝突? – 2010-12-10 03:12:59
它們應該始終是唯一的 – 2010-12-10 03:17:05