简体   繁体   中英

How to add an element To Array

Hmm.. thats realy different problem I fetch $links from db linke this

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

The array looks like this

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"
  }

I have a function which returns the pagerank of a given url GooglePagerank($url);

now how can i add the pagerank to the above array and assign them to smarty? Thanks

You can add new elements to the array with the [] operator, if you define a valid unique key name. Your data is stored in the $links array. Just add it like this:

$links['PAGERANK'] = $yourPageRankvar;

In your case it would be something like this:

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

Assign it to Smarty like this:

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

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

hmm.. that was easier as i thought

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

thats it

as result

 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"
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM