English 中文(简体)
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

最佳回答

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"
  }
问题回答

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

You can assign the result of GooglePagerank() to $array[ pagerank ], for example?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签