2010-12-10 68 views
4

這可能是某種怪異的長快捷方式,並請糾正我,如果我在這個思路錯了......由多個指標參考PHP數組

我有,看起來像數據矩陣:

unique_id | url | other random data... 
unique_id | url | other random data... 
unique_id | url | other random data... 

我想能夠引用一個項目的URL或它的unique_id - 有沒有一種奇特的方式來做到這一點?

我想作弊的解決辦法是隻做兩個陣列,但我想知道是否有更好的方法。

+2

不是真的,沒有。只需製作兩個陣列。開銷很小(很好,取決於數組的大小)。 – 2010-12-10 03:12:24

+0

你需要修改「隨機數據」,一旦它在數組中?而且它有可能造成unique_id和URL發生衝突? – 2010-12-10 03:12:59

+0

它們應該始終是唯一的 – 2010-12-10 03:17:05

回答

1

嘗試是這樣的:

function selectByIdOrURL($array, $data) { 
    foreach($array as $row) { 
     if($row['unique_id'] == $data || $row['url'] == $data) return $row; 
    } 
    return NULL; 
} 

$array = array(
      array('unique_id' => 5, 'url' => 'http://blah.com'), 
      array('unique_id' => 3, 'url' => 'http://somewhere_else.com') 
     ); 
$found = selectByIdOrURL($array, 5); //array('unique_id' => 5, 'url' => 'http://blah.com') 
$nfound = selectByIdOrURL($array, 10); //NULL 
9

我能想到的唯一的辦法不涉及遍歷數組的每個搜索(見雅各布的回答)是存儲在兩個數組的每個項目的引用。

編輯:由於URL和ID不能發生碰撞,它們可以被存儲在相同的陣列中(感謝馬修)

$items; // array of item objects 
     // Use objects so they're implicitly passed by ref 

$itemRef = array(); 

foreach ($items as $item) { 
    $itemRef[$item->unique_id] = $item; 
    $itemRef[$item->url] = $item; 
} 

// find by id 
$byId = $itemRef[$id]; 

// find by url 
$byUrl = $itemRef[$url]; 

你也許可以封裝這個很好的使用實現getById()和集合類getByUrl()。在內部,它可以將引用存儲在儘可能多的數組中。

當然,你在本質上正在做的是創建索引結果集,最好留給數據庫管理系統。

0

當然,一個對象會是簡單的方法嗎?

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"); 

使用這種技術,由於對象創建一些額外的開銷,但除非你做行瘋狂的數字,將被罰款。

1

看起來您的奇特解決方案僅適用於PHP 5.5。 您可以結合使用array_searcharray_column在一個單一的代碼行獲取您的輸入:

$items = [ 
    [ 
    'unique_id' => 42, 
    'url' => 'http://foo.com' 
    ], 
    [ 
    'unique_id' => 57, 
    'url' => 'http://bar.com' 
    ], 
    [ 
    'unique_id' => 36, 
    'url' => 'http://example.com' 
    ], 

]; 

$bar = $entries[array_search(57, array_column($items, 'unique_id'))]; 

var_dump($bar); 

//outputs 
array (size=2) 
    'unique_id' => int 57 
    'url' => string 'http://bar.com' (length=14)