Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active October 19, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayeenulislam/a151c1981406173fe7d9 to your computer and use it in GitHub Desktop.
Save mayeenulislam/a151c1981406173fe7d9 to your computer and use it in GitHub Desktop.
Taking right value from the existing and new. DIRECTION: Activate one block per page load, and see the changes.
<?php
echo '<h3>1st time</h3>'; //===================================== 1st
$existing_locations = null;
$new_locations = array( 1,2,3 ); //+3 (1,2,3)
if( is_array( $existing_locations ) ) {
$new_diff = array_diff( $new_locations, $existing_locations );
$existing_diff = array_diff( $existing_locations, $new_locations );
} else {
$new_diff = $new_locations;
$existing_diff = $existing_locations;
}
if( $new_diff ) {
foreach( $new_diff as $new_location ) {
//add_post_meta( $post_id, "{$project_prefix}post_locations", $new_location );
echo "New Multiple: ", $new_location, ' (', gettype($new_location) ,')', '<br>';
}
}
if( $existing_locations && $existing_diff ) {
if( is_array( $existing_diff ) ) {
foreach ( $existing_diff as $existing_location ) {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_location );
echo "Delete Multiple: ", $existing_location, ' (', gettype($existing_location) ,')', '<br>';
}
} else {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_diff );
echo "Delete Single: ", $existing_diff, ' (', gettype($existing_diff) ,')', '<br>';
}
}
echo '<hr>';
echo '<h3>2nd time</h3>'; //===================================== 2nd
/*$existing_locations = array( 1,2,3 );
$new_locations = array( 1,2 ); //-1 (3)
if( is_array( $existing_locations ) ) {
$new_diff = array_diff( $new_locations, $existing_locations );
$existing_diff = array_diff( $existing_locations, $new_locations );
} else {
$new_diff = $new_locations;
$existing_diff = $existing_locations;
}
if( $new_diff ) {
foreach( $new_diff as $new_location ) {
//add_post_meta( $post_id, "{$project_prefix}post_locations", $new_location );
echo "New Multiple: ", $new_location, ' (', gettype($new_location) ,')', '<br>';
}
}
if( $existing_locations && $existing_diff ) {
if( is_array( $existing_diff ) ) {
foreach ( $existing_diff as $existing_location ) {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_location );
echo "Delete Multiple: ", $existing_location, ' (', gettype($existing_location) ,')', '<br>';
}
} else {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_diff );
echo "Delete Single: ", $existing_diff, ' (', gettype($existing_diff) ,')', '<br>';
}
}
echo '<hr>';*/
echo '<h3>3rd time</h3>'; //===================================== 3rd
/*$existing_locations = array( 1,2 );
$new_locations = array( 1,2 ); //0
if( is_array( $existing_locations ) ) {
$new_diff = array_diff( $new_locations, $existing_locations );
$existing_diff = array_diff( $existing_locations, $new_locations );
} else {
$new_diff = $new_locations;
$existing_diff = $existing_locations;
}
if( $new_diff ) {
foreach( $new_diff as $new_location ) {
//add_post_meta( $post_id, "{$project_prefix}post_locations", $new_location );
echo "New Multiple: ", $new_location, ' (', gettype($new_location) ,')', '<br>';
}
}
if( $existing_locations && $existing_diff ) {
if( is_array( $existing_diff ) ) {
foreach ( $existing_diff as $existing_location ) {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_location );
echo "Delete Multiple: ", $existing_location, ' (', gettype($existing_location) ,')', '<br>';
}
} else {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_diff );
echo "Delete Single: ", $existing_diff, ' (', gettype($existing_diff) ,')', '<br>';
}
}
echo '<hr>';*/
echo '<h3>4th time</h3>'; //===================================== 4th
/*$existing_locations = array( 1,2 );
$new_locations = array( 1,2,5 ); //+1 (5)
if( is_array( $existing_locations ) ) {
$new_diff = array_diff( $new_locations, $existing_locations );
$existing_diff = array_diff( $existing_locations, $new_locations );
} else {
$new_diff = $new_locations;
$existing_diff = $existing_locations;
}
if( $new_diff ) {
foreach( $new_diff as $new_location ) {
//add_post_meta( $post_id, "{$project_prefix}post_locations", $new_location );
echo "New Multiple: ", $new_location, ' (', gettype($new_location) ,')', '<br>';
}
}
if( $existing_locations && $existing_diff ) {
if( is_array( $existing_diff ) ) {
foreach ( $existing_diff as $existing_location ) {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_location );
echo "Delete Multiple: ", $existing_location, ' (', gettype($existing_location) ,')', '<br>';
}
} else {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_diff );
echo "Delete Single: ", $existing_diff, ' (', gettype($existing_diff) ,')', '<br>';
}
}
echo '<hr>';*/
echo '<h3>5th time</h3>'; //===================================== 5th
/*$existing_locations = array( 1,2,5 );
$new_locations = array( 1,5,4,7 ); //-1 (2), +2 (4,7)
if( is_array( $existing_locations ) ) {
$new_diff = array_diff( $new_locations, $existing_locations );
$existing_diff = array_diff( $existing_locations, $new_locations );
} else {
$new_diff = $new_locations;
$existing_diff = $existing_locations;
}
if( $new_diff ) {
foreach( $new_diff as $new_location ) {
//add_post_meta( $post_id, "{$project_prefix}post_locations", $new_location );
echo "New Multiple: ", $new_location, ' (', gettype($new_location) ,')', '<br>';
}
}
if( $existing_locations && $existing_diff ) {
if( is_array( $existing_diff ) ) {
foreach ( $existing_diff as $existing_location ) {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_location );
echo "Delete Multiple: ", $existing_location, ' (', gettype($existing_location) ,')', '<br>';
}
} else {
//delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_diff );
echo "Delete Single: ", $existing_diff, ' (', gettype($existing_diff) ,')', '<br>';
}
}
echo '<hr>';*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment