什么样的PHP数据结构适合存储网址和描述等


What PHP data structure suits the storage of web urls and descriptions etc?

我想在PHP中的某种结构或数组中存储以下信息;

  • URL
  • 标题
  • 说明
  • 排名

我希望数据是关联的,一个特定的URL指的是一个标题,描述&等级

我希望能够按等级对数据进行排序,然后按该顺序对其进行回显,并且每个元素仍然关联。

我应该使用关联数组吗?结构?或者其他一些PHP数据结构?

感谢

$urls["http://example.com"] = array(
    "rank" => 3,
    "title" => "abc",
    ...
)

并使用uasort订购

我会为此创建一个自定义类。有关php类的更多信息,请访问php.net手册。

您可以尝试:

$data = new LinkData();
$data->set("http://stackoverflow.com/q/17406624/1226894", [
        "name" => "Data Structure",
        "rank" => 3
]);
$data->set("http://stackoverflow.com/", [
        "desc" => "Nice Site",
        "title" => "Stackoverflow"
]);
foreach($data as $v) {
    print_r($v);
}

输出

Array
(
    [url] => http://stackoverflow.com/q/17406624/1226894
    [title] => 
    [rank] => 3
    [desc] => 
    [name] => Data Structure
)
Array
(
    [url] => http://stackoverflow.com/
    [title] => Stackoverflow
    [rank] => 
    [desc] => Nice Site
)

分类使用

class LinkData implements IteratorAggregate {
    private $data = array();
    function getIterator() {
        return new ArrayIterator($this->data);
    }
    function set($url, array $info) {
        $this->data[md5($url)] = array_merge([
                "url" => $url,
                "title" => null,
                "rank" => null,
                "desc" => null
        ], $info);
    }
    function get($url) {
        return isset($this->data[$key = md5($url)]) ? $this->data[$key] : [];
    }
}