How to import meta for WooCommerce multiple products

In some situations, you have to import some meta for hundreds of products. Then, manually it can do by
1. Open a product.
2. Update the meta.
3. Update the product.
If there are hundreds of products, we have to repeat this process a hundred times. Which is really complex to maintain.

There are plugins for import custom meta for existing products. But, they are costly.

There is a way that will save repeating work and save cost. Here it is:

Continue reading “How to import meta for WooCommerce multiple products”

About strictly increasing sequence

Problem:
You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.

Solution:

function arrayChange($inputArray) {
    $count = 0;
        
    foreach ( $inputArray as $key => $value ) {
        $is_less = isset( $key ) && isset( $inputArray[ $key-1 ] ) && $value <= $inputArray[ $key-1 ];
        if ( $is_less ) {        
            $inputArray[ $key ] = ( $inputArray[ $key - 1 ] ) + 1;
            $count += $inputArray[ $key-1 ]- $value + 1;
        }
    }
  
    return $count;
}

Calculator in PHP

To develop a simple calculator class in the context of PHP is more fun with __call() magic method.

class Calculator {
    public function __call($method, $args) {
        if (isset($this->$method)) {
            $func = $this->$method;
            return call_user_func_array($func, $args);
        }
    }
}

$calculator = new Calculator();
$calculator->add = function ( $a1, $a2 ) { echo $a1 + $a2; };
$calculator->subs = function( $a1, $a2 ) { echo $a1 - $a2; };
$calculator->multi = function( $a1, $a2 ) { echo $a1 * $a2; };
$calculator->div = function( $a1, $a2 ) { echo $a1 / $a2; };

Is two arrays are similar?

Problem:

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.

Given two arrays a and b, check whether they are similar.

Example

  • For a = [1, 2, 3] and b = [1, 2, 3], the output should be
    areSimilar(a, b) = true.The arrays are equal, no need to swap any elements.
  • For a = [1, 2, 3] and b = [2, 1, 3], the output should be
    areSimilar(a, b) = true.We can obtain b from a by swapping 2 and 1 in b.
  • For a = [1, 2, 2] and b = [2, 1, 1], the output should be
    areSimilar(a, b) = false.Any swap of any two elements either in a or in b won’t make a and b equal.

Solution:

function areSimilar($a, $b) {
    $array_difference = array_diff_assoc($a, $b);
    if( empty( $array_difference )) return true;
    if( count($array_difference) === 2) {
        $b_a_array_difference = array_diff_assoc($b, $a);
        $is_equal = array_diff($array_difference, $b_a_array_difference);
        if(!empty($is_equal)) {
            return false;
        }
        return true;
    }
    return false;
}

Counting Vowels

In JavaScript:

let str =
  "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
const vowels = /[aeiouy]/g;
let vowelCount = str.match(vowels).length;
console.log(`Total Vowel ${vowelCount}`);

Output: Total Vowel 174

In PHP

$str = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
$vowels = "/[aeiouy]/";
echo $vowel_count = preg_match_all( $vowels, $str );

Output: 174

WordPress install with Docker

Docker enables to separate your applications from your infrastructure so you can deliver software quickly.

This post aims to setup WordPress with Docker Compose. Why Docker Compose?

With Compose, you use a YAML file to configure your application’s services.

docker-compose.yaml or docker-compose.yml is file where services are defined. So, lets crack the nuts.

Continue reading “WordPress install with Docker”