yit_avoid_duplicate

Check if something exists.

Description

yit_avoid_duplicate( (mixed) $value, (array) $array, (string) $check = 'value' ); 

If yes, add a -N to the value where N is a number.

Parameters (3)

0. $value (mixed)
The value.
1. $array (array)
The array.
2. $check — Optional. (string) => 'value'
The check.

Usage

  1. if ( !function_exists( 'yit_avoid_duplicate' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'yith-woocommerce-compare/plugin-fw/yit-functions.php'; 
  3.  
  4. // The value. 
  5. $value = null; 
  6.  
  7. // The array. 
  8. $array = array(); 
  9.  
  10. // The check. 
  11. $check = 'value'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = yit_avoid_duplicate($value, $array, $check); 
  15.  

Defined (1)

The function is defined in the following location(s).

/plugin-fw/yit-functions.php  
  1. function yit_avoid_duplicate ( $value, $array, $check = 'value' ) { 
  2. $match = array (); 
  3.  
  4. if ( ! is_array ( $array ) ) { 
  5. return $value; 
  6.  
  7. if ( ( $check == 'value' && ! in_array ( $value, $array ) ) || ( $check == 'key' && ! isset( $array[ $value ] ) ) ) { 
  8. return $value; 
  9. } else { 
  10. if ( ! preg_match ( '/([a-z]+)-([0-9]+)/', $value, $match ) ) { 
  11. $i = 2; 
  12. } else { 
  13. $i = intval ( $match[ 2 ] ) + 1; 
  14. $value = $match[ 1 ]; 
  15.  
  16. return yit_avoid_duplicate ( $value . '-' . $i, $array, $check );