unkwntech Posted October 9, 2008 Share Posted October 9, 2008 I'm beating my head against the desk with this one. I have an array, the end of the array looks like this: [2389] => 127.0.0.1 zanox.com [2390] => 127.0.0.1 zeads.com [2391] => 127.0.0.1 zedo.com [2392] => 127.0.0.1 zencudo.co.uk [2393] => 127.0.0.1 zenzuu.com [2394] => 127.0.0.1 zeus.developershed.com [2395] => 127.0.0.1 zintext.com [2396] => 127.0.0.1 zmedia.com [2397] => [2398] => [2399] => [2400] => [2401] => [2402] => [2403] => I have this code, that is processing the array before it is outputted: for($i=0;$i<count($file);$i++) { if(strlen($file[$i]) < 1) { unset($file[$i]); } } If I echo strlen($file[2403]) I get zero, but obviously the above loop should be removing it. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/ Share on other sites More sharing options...
JasonLewis Posted October 9, 2008 Share Posted October 9, 2008 It would be better to do a print_r() afterwards as well. This is how I would do it though. for($i = 0; $i < count($file); $i++){ if(strlen($file[$i]) < 1){ $file = array_splice($file, $i, 1); } } Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660689 Share on other sites More sharing options...
unkwntech Posted October 9, 2008 Author Share Posted October 9, 2008 The copy of the array that I pasted was from a print_r after the loop. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660705 Share on other sites More sharing options...
Andy-H Posted October 9, 2008 Share Posted October 9, 2008 Also in an array with that many values I wouldn't run the count() function inside the loop. Assign the number to a variable first then use the variable in the loop, should be a little faster. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660716 Share on other sites More sharing options...
unkwntech Posted October 9, 2008 Author Share Posted October 9, 2008 I'm not calling count() inside the loop. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660719 Share on other sites More sharing options...
Andy-H Posted October 9, 2008 Share Posted October 9, 2008 I'm beating my head against the desk with this one. I have an array, the end of the array looks like this: [2389] => 127.0.0.1 zanox.com [2390] => 127.0.0.1 zeads.com [2391] => 127.0.0.1 zedo.com [2392] => 127.0.0.1 zencudo.co.uk [2393] => 127.0.0.1 zenzuu.com [2394] => 127.0.0.1 zeus.developershed.com [2395] => 127.0.0.1 zintext.com [2396] => 127.0.0.1 zmedia.com [2397] => [2398] => [2399] => [2400] => [2401] => [2402] => [2403] => I have this code, that is processing the array before it is outputted: for($i=0;$i<count($file);$i++) /*???*/ { if(strlen($file[$i]) < 1) { unset($file[$i]); } } If I echo strlen($file[2403]) I get zero, but obviously the above loop should be removing it. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660722 Share on other sites More sharing options...
unkwntech Posted October 9, 2008 Author Share Posted October 9, 2008 @Andy-H: The following: <?php $array = range(0, 100000); $start1 = microtime(TRUE); for($i=0;$i<count($array);$i++) { //do something } $time1 = microtime(TRUE) - $start1; $start2 = microtime(TRUE); $count = count($array); for($i=0;$i<$count;$i++) { //do something } $time2 = microtime(TRUE) - $start2; echo 'Time 1: ' . $time1 . ' Time 2: ' . $time2; ?> outputs the following on my system: Time 1: 0.82814311981201 Time 2: 0.84739398956299 So indeed my way was faster. I don't mean this to be argumentative, I just wanted to verify what you said and these are the results. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660725 Share on other sites More sharing options...
Andy-H Posted October 9, 2008 Share Posted October 9, 2008 lol how does that work out? Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660746 Share on other sites More sharing options...
unkwntech Posted October 9, 2008 Author Share Posted October 9, 2008 IDK but thats what I get. I'm still having trouble with my script though. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660748 Share on other sites More sharing options...
Andy-H Posted October 9, 2008 Share Posted October 9, 2008 $str = ""; for ($i = 0; $i < count($file); $i++){ if ( strlen($file[$i]) > 0 ){ $str .= "-" . $file[$i]; } } $str = substr($str, 1); $file = implode("-", $str); Does that get the desired result? Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660752 Share on other sites More sharing options...
JasonLewis Posted October 9, 2008 Share Posted October 9, 2008 Why not something like this? $array = array("127.0.0.1 zanox.com", "127.0.0.1 zeads.com", "127.0.0.1 zedo.com", "127.0.0.1 zencudo.co.uk", "127.0.0.1 zenzuu.com", "127.0.0.1 zeus.developershed.com", "127.0.0.1 zintext.com", "127.0.0.1 zmedia.com", "", "", "", "", "", "", "", ); $tmp_array = array(); for($i = 0; $i < count($array); $i++){ if($array[$i] != ""){ $tmp_array[$i] = $array[$i]; } } $array = $tmp_array; echo "<pre>", print_r($array), "</pre>"; Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660763 Share on other sites More sharing options...
Andy-H Posted October 9, 2008 Share Posted October 9, 2008 lol yup that would be easier, duno if he neccessarily wanted to keep the same keys tho maybe $tmp_array[] = $array[$i] Would be better Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660765 Share on other sites More sharing options...
JasonLewis Posted October 9, 2008 Share Posted October 9, 2008 Yeah, either way. I just assumed the keys were to stay the same. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660766 Share on other sites More sharing options...
unkwntech Posted October 9, 2008 Author Share Posted October 9, 2008 @ProjectFear - Thanks its finally working. Link to comment https://forums.phpfreaks.com/topic/127663-solved-little-help/#findComment-660782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.