sandy1028 Posted May 20, 2009 Share Posted May 20, 2009 Hi, Array_diff prints the array. When used in for loop to print an array and output when redirected to a text file, it contains the blank line in between. How to remove the blank lines Quote Link to comment Share on other sites More sharing options...
trq Posted May 20, 2009 Share Posted May 20, 2009 Post your code. array_diff does not print anything. Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted May 20, 2009 Author Share Posted May 20, 2009 $result = array_diff($array1, $array2); for($i=0;$i<=count($result);$i++){ echo "$result[$i]\n"; } o/p abc dcg ehh How to remove the lines in between these lines Quote Link to comment Share on other sites More sharing options...
Jibberish Posted May 20, 2009 Share Posted May 20, 2009 Try this. Had a quick look arround php.net and empty() looks like the function you need to check to see if the array element it empty. <?php $result = array_diff($array1, $array2); for($i=0;$i<=count($result);$i++){ if(!empty($result[$i])) { echo "$result[$i]\n"; } } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 20, 2009 Share Posted May 20, 2009 Had a quick look arround php.net and empty() looks like the function you need to check to see if the array element it empty. Yeah that's correct. However, in this case, those entries aren't actually empty -- they don't exist. The array_diff function maintains association between keys and values of the arrays - that is, the elements in $result have the same keys as they had in $array1. Therefore, you might have an array with, say, keys 0, 2, 5 and 8. The length of the array is 4, but only the elements with keys 0 and 2 will be printed, plus blank likes for the non-existing elements with keys 1 and 3. If that's not very clear, use print_r on the generated array and you'll see what i mean. The solution? Use a foreach loop instead: foreach($result as $value){ echo $value . "\n"; } Quote Link to comment Share on other sites More sharing options...
Jibberish Posted May 20, 2009 Share Posted May 20, 2009 But if the arrays look like this, <?php $array1 = array(0=>'abc', 1=>"", 2=>"", 3=>'wqd'); $array2 = array(0=>'abc', 1=>'sdf', 2=>'fgs', 3=>'wqsd'); $result = array_diff($array1, $array2); print_r($result); ?> the result is, Array ( [1] => [2] => [3] => wqd ) , so wont the foreach just do the same thing? i.e. put in the blank spaces. Yeah I see what you mean with an associative array, but I suppose this example depends on how they arrays are indexed in the first place. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 20, 2009 Share Posted May 20, 2009 Yeah, of course, if the values of the arrays themselves are empty strings, you'd get blank lines printed out. But then, if the arrays contained blank lines, i'd expect that the OP would want them to be printed. the result is, Array ( [1] => [2] => [3] => wqd ) , so wont the foreach just do the same thing? i.e. put in the blank spaces. However, the point is that, even with the above example, the required information wouldn't be printed if you used a for loop. If we have the following code (which is actually just the original) : for($i=0;$i<count($result);i++){ echo $array[$i]; } Then what's printed? first array[0]. That doesn't exist. If you have notices turned on, you'd get a warning. If you dont, we'll have an extra new line. Then array[1] and array[2] are printed. But array[3] isn't printed - our loop finishes when i = 2, since there are 3 elements in the array. So all in all, you really should be using a foreach loop. Quote Link to comment Share on other sites More sharing options...
Jibberish Posted May 20, 2009 Share Posted May 20, 2009 Ah, thanks for clearing that up, still learning php myself so wasn't to sure at first what you meant. But now I see what you mean, by useing a for loop and count() it miss array elements out if the indexs didnt start at 0 or finished after the return value of count(), wheras the foreach loop just uses every index in an array. Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.