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 Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/ 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. Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-837930 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 Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-837938 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-837951 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"; } Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-837954 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. Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-837963 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. Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-837994 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. Link to comment https://forums.phpfreaks.com/topic/158873-array_diff/#findComment-838023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.