Web Development & WordPress

When to Use array_walk() and array_map() in PHP: Working with Individual Array Elements

array_walk() and array_map() both are used to work with the individual array element. array_walk() allows for operations to be performed on each element, but it does not return a new array. Instead, it operates directly on the original array and is typically used for side effects like printing, modifying elements in-place, or other operations where the result is not needed in a new array.

On the other hand, array_map() Returns a new array with modified values.

Continue reading “When to Use array_walk() and array_map() in PHP: Working with Individual Array Elements”
Web Development & WordPress

Learning Plugins Development: Part 3

Here i am trying to create a custom WordPress plugin that generates a shortcode [post_title_with_pagination] to display post titles with pagination. This step-by-step guide will help you set up the plugin and use it in your WordPress site effectively. Please check Learning Plugins Development: Part 1 to find out the steps.

Continue reading “Learning Plugins Development: Part 3”
Web Development & WordPress

Sorting Arrays in PHP: rsort() and arsort() Functions Explained

This PHP script provides an example of how to use rsort() for reverse sorting and arsort() for reverse sorting while preserving the array keys.

<?php
echo "<pre>";
$reverseSort= array("a","d","e","b","c");
echo " <br> Main array <br>";
print_r($reverseSort);
rsort($reverseSort);
echo"<br>After rsort():<br>";
print_r($reverseSort);

$reverseSort1= array("a","d","e","b","c");
echo " <br> Main array <br>";
print_r($reverseSort1);

echo"<br>After rasort() which preserve keys :<br>";
arsort($reverseSort1);
print_r($reverseSort1);

echo "</pre>";
?>

OUTPUT

 Main array 
Array
(
    [0] => a
    [1] => d
    [2] => e
    [3] => b
    [4] => c
)

After rsort():
Array
(
    [0] => e
    [1] => d
    [2] => c
    [3] => b
    [4] => a
)
 
 Main array 
Array
(
    [0] => a
    [1] => d
    [2] => e
    [3] => b
    [4] => c
)

After rasort() which preserve keys :
Array
(
    [2] => e
    [1] => d
    [4] => c
    [3] => b
    [0] => a
)