Page 1 of 1

working with an array-- need help setting up the code

Posted: Sun Nov 18, 2007 7:34 pm
by irishmike2004
Greetings all:

I am working with Del.icio.us tags in a PHP script. Have the script functioning as I would like it to.

Now I need to work on an array I build in the script where a variable to limit the number of tags we actually ultimately display is imposed upon the array.

For example: if my array ($tags) has 242 elements in it I want just the first number of them as defined by the variable ($limit). Now the tricky part is... IF the number of elements (count($tags);) is greater than the limit then we delete the elements from the array after that number.

E.G. If $limit = 25 then we would keep the first 25 array elements of the 242.

IF $limit =0 or count($tags) is less than $limit we process normally.

With this is mind here is the script:

Code: Select all

<?php
//Del.icio.us User Name
$userId = "del.ico.us user name";

//process the JS feed (TagRoll)
$url = "http://del.icio.us/feeds/js/tags/".$userId;
$read = file_get_contents($url);

//run the match and build the tags array
$match = '/"(.*?)":/';
preg_match_all($match, $read, $tags);
$tags = $tags[1];

//count of the number of elements in the tags array
$tagCount = count($tags);

//this is a temporary echo statement to check number of tags
//echo 'Tag Count is '.$tagCount.'<br />'; 

//process for output
echo '<LINK REL="StyleSheet" HREF="deltags.css" TYPE="text/css">';
echo'<div class="delicious-tags" id="delicious-tags-'.$userId.'"><ul class="delicious-cloud">TAGS: ';

foreach ($tags as $tag) {
       echo '<li class="delicous-tags"><a href="http://del.icio.us/'.$userId.'/'.$tag.'">'.$tag.'</a></li>';
}

echo '</ul></div>';

?>
I am thinking this could a simple IF / ELSE, but probably a function would be nicer. I am also willing to take suggestions on "prettifying" this code or if I should put anything into function... but it works except I need to be able to display a limited number of the tags returned (eg we only want to show the first 25 for now, but I want this to be able to be changed. Basically IF $limit = 0 it functions just as it does now... and the case where like my user id (nitromike) has only 1 tag for now, obviously we would not process 25 times for it... so as long as the number of tags is less than the limit number it would just print them as the script does... if more,it cuts the data after the 25th tag out of the array before processing it.

The cutting the data from the tag is what I REALLY need to know how to do...

Thanks for your help in advance.

And again, advice welcome :-)

Have a good day!

Mike

Posted: Sun Nov 18, 2007 8:18 pm
by Jonah Bron
I have wondered myself how to delete parts of arrays. I think you would want something like this:

Code: Select all

while ($tagCount > $limit){
  //delete_arr() is a fictitcous function
  $tags = delete_arr($tags[$tagCount-1]);
}

Posted: Sun Nov 18, 2007 8:22 pm
by irishmike2004
@everyone:

Thanks for the help.

I got it figured out -- here is the code I used:

Code: Select all

if($tagCount > $limit && $limit != 0){

	$startCut = $limit -1;
		
	for($i=$tagCount;$i != $startCut;$i--){
		unset($tags[$i]);
	}
}
$limit is set elsewhere in the PHP script. Basically it is starting from the last element and working to the one just before the limit number in effect, deleting element 26-242 in this case.

Thanks,

Mike