Jump to content

array_diff


sandy1028

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.