如何将元素添加到数组

时间:2009-11-27 22:48:11

标签: php arrays smarty

嗯..那确实是不同的问题 我从db获取$ links 林克这个

$ links = $ db-> GetAll(“SELECT * FROM {$tables['link']['name']} WHERE STATUS ='2'AND CATEGORY_ID =”。$ db-> qstr($ id )。“{$ feat_where} {$ expire_where} ORDER BY {$sort_cols[$sort]} {$ sort_ord [$ sort]} {$ limit}”);

数组看起来像这样

array(28) {
    ["ID"]=>
    string(1) "3"
    ["TITLE"]=>
    string(6) "Google"
    ["DESCRIPTION"]=>
    string(6) "Google"
    ["URL"]=>
    string(21) "http://www.google.com"
    ["CATEGORY_ID"]=>
    string(1) "2"
    ["RECPR_URL"]=>
    string(0) ""
    ["RECPR_REQUIRED"]=>
    string(1) "0"
    ["STATUS"]=>
    string(1) "2"
    ["VALID"]=>
    string(1) "1"
    ["RECPR_VALID"]=>
    string(1) "1"
    ["OWNER_ID"]=>
    NULL
    ["OWNER_NAME"]=>
    string(0) ""
    ["OWNER_EMAIL"]=>
    string(0) ""
    ["OWNER_NOTIF"]=>
    string(1) "0"
    ["DATE_MODIFIED"]=>
    string(19) "2009-11-27 13:30:07"
    ["DATE_ADDED"]=>
    string(19) "2009-11-27 13:30:07"
    ["HITS"]=>
    string(1) "0"
    ["LAST_CHECKED"]=>
    NULL
    ["RECPR_LAST_CHECKED"]=>
    NULL
    ["PAGERANK"]=>
    string(2) "0"
    ["RECPR_PAGERANK"]=>
    string(2) "-1"
    ["FEATURED_MAIN"]=>
    string(1) "0"
    ["FEATURED"]=>
    string(1) "0"
    ["EXPIRY_DATE"]=>
    NULL
    ["NOFOLLOW"]=>
    string(1) "0"
    ["PAYED"]=>
    string(2) "-1"
    ["LINK_TYPE"]=>
    string(1) "0"
    ["IPADDRESS"]=>
    string(13) "80.219.78.155"
  }

我有一个返回给定网址的pagerank的函数 GooglePagerank($ URL);

现在我如何将pagerank添加到上面的数组并将它们分配给smarty? 谢谢

3 个答案:

答案 0 :(得分:0)

如果定义了有效的唯一键名,则可以使用[]运算符向数组中添加新元素。您的数据存储在$ links数组中。只需添加如下:

$links['PAGERANK'] = $yourPageRankvar;

在你的情况下,它会是这样的:

$links['PAGERANK'] = GooglePagerank($links["URL"]);

像这样将它分配给Smarty:

$smarty->assign('name', $links);

答案 1 :(得分:0)

您可以将GooglePagerank()的结果分配给$array['pagerank'],例如?

答案 2 :(得分:0)

嗯......我想的更容易

 for($i=0;$i<count($links);$i++)
      {
        $links[$i]["PAGERANK"] = GooglePagerank($links[$i]["URL"]);
      }

多数民众赞成

结果

 array(28) {
    ["ID"]=>
    string(1) "3"
    ["TITLE"]=>
    string(6) "Google"
    ["DESCRIPTION"]=>
    string(6) "Google"
    ["URL"]=>
    string(21) "http://www.google.com"
    ["CATEGORY_ID"]=>
    string(1) "2"
    ["RECPR_URL"]=>
    string(0) ""
    ["RECPR_REQUIRED"]=>
    string(1) "0"
    ["STATUS"]=>
    string(1) "2"
    ["VALID"]=>
    string(1) "1"
    ["RECPR_VALID"]=>
    string(1) "1"
    ["OWNER_ID"]=>
    NULL
    ["OWNER_NAME"]=>
    string(0) ""
    ["OWNER_EMAIL"]=>
    string(0) ""
    ["OWNER_NOTIF"]=>
    string(1) "0"
    ["DATE_MODIFIED"]=>
    string(19) "2009-11-27 13:30:07"
    ["DATE_ADDED"]=>
    string(19) "2009-11-27 13:30:07"
    ["HITS"]=>
    string(1) "0"
    ["LAST_CHECKED"]=>
    NULL
    ["RECPR_LAST_CHECKED"]=>
    NULL
    ["PAGERANK"]=>
    string(2) "10"
    ["RECPR_PAGERANK"]=>
    string(2) "-1"
    ["FEATURED_MAIN"]=>
    string(1) "0"
    ["FEATURED"]=>
    string(1) "0"
    ["EXPIRY_DATE"]=>
    NULL
    ["NOFOLLOW"]=>
    string(1) "0"
    ["PAYED"]=>
    string(2) "-1"
    ["LINK_TYPE"]=>
    string(1) "0"
    ["IPADDRESS"]=>
    string(13) "80.219.78.155"
  }