Hi!

I tried hard to find a solution to a problem I'm going to explain here, and after have read all the array functions and possibilities, I had to create what I think should exist on next PHP releases.



What I needed, it's some kind of Difference, but working with two arrays and modifying them at time, not returning an array as a result with the diference itself.



So, as an example:



A = 1,2,3

B = 2,3,4



should NOT be:



C = 1,4



but:



A = 1

B = 4



so basically, I wanted to delete coincidences on both arrays.



Now, I've some actions to do, and I know wich one I've to do with the values from one array or another.

With the normal DIFF I can't, because if I've an array like C=1,4, I dont know if I've to do the Action_A with 1 or with 4, but I really know that everything in A, will go to the Action_A and everithing in B, will go to Action_B. So same happens with 4, don't know wich action to apply...



So I created this:



<?php

function array_diff_ORG_NEW (& $org , & $new , $type = 'VALUES' ){

switch( $type ){

case 'VALUES' :

$int = array_values ( array_intersect ( $org , $new )); $org = array_values ( array_diff ( $org , $int )); $new = array_values ( array_diff ( $new , $int )); break;

case 'KEYS' :

$int = array_values ( array_intersect_key ( $org , $new )); $org = array_values ( array_diff_key ( $org , $int )); $new = array_values ( array_diff_key ( $new , $int )); break;

}

}

?>



This cute, works by reference, and modifies the arrays deleting coincidences on both, and leaving intact the non coincidences.



So a call to this will be somethin' like:



<?php

$original = array( 1 , 2 , 3 );

$new = array( 2 , 3 , 4 );



array_diff_ORG_NEW ( $original , $new , 'VALUES' );

?>



And HERE, I'll have my arrays as I wanted:



$original = 1

$new = 4



Now, why I use it precisely?



Imagine you've some "Events" and some users you select when create the event, can "see" this event you create. So you "share" the event with some users. Ok?



Imagine you created and Event_A, and shared with users 1,2,3.



Now you want to modify the event, and you decide to modify the users to share it. Imagine you change it to users 2,3,4.



(numbers are users ID).



So you can manage when you are going to modify, to have an array with the IDs in DDBB ($original), and then, have another array with ID's corresponding to the users to share after modifying ($new). Wich ones you've to DELETE from DDBB, and wich ones do you've to INSERT?



If you do a simple difference or somehow, you get somethin' like C=1,4.

You have no clue on wich one you've to insert or delete.



But on this way, you can know it, and that's why:



- What keeps on $original, it's somethin not existing in $new at the beggining. So you know that all what you've inside $original, have to be deleted from DDBB because what you did in the modifying process, it's to unselect those users keeping in $original.

- What keeps on $new, it's something not existing in $original at the beggining. Wich means that in the modifying process you added some new users. And those have to be inserted in DDBB. So, everything keeping inside $new, have to be inserted in the DDBB.



Conclusion:



- Remaining in $original --> delete from DB.

- Remaining in $new --> insert into DB.



And that's all!



I hope you find it useful, and I encourage PHP "makers", to add in a not distant future, somethin' like this one natively, because I'm shure that I'm not the first one needing something like this.



Best regards all,



Light.