Joseph Downey PHP Example

Arrays Example 3

Enter Values to Store in an Array

Select Modifications:







Results:

Results will appear here...
 

• Copyright© 2025 xPralak Designs - Joseph Downey

 

<?php

// =====================================================================================
// =====================================================================================
// =====================================================================================
// ===================  xPralak Designs - Joe Downey                 ===================
// ===================  Developed - February, 24th 2025              ===================
// ===================  Version - PHP 8.4                            ===================
// ===================  Class - Web Development PHP & MySQL - 80524  ===================
// =====================================================================================
// =====================================================================================
// =====================================================================================
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
$input $_POST['values'] ?? '';

    
// If input is "test", use predefined array
    
if (strtolower($input) === "test") {
        
$array = ["Apple""Banana""Car""Dog""Elephant""Fire""Guitar""House""Ice""Jacket""Kite""Laptop""Mountain""Notebook""Orange""Pencil""Queen""Rocket""Sun"];
    } else {
        
$array array_map('trim'explode('|'$input));
    }

    
// Merge with another array (if selected)
    
if (!empty($_POST['merge'])) {
        
$extraArray = ["Tiger""Mountain""Bridge""Lantern""River""Cactus""Galaxy""Castle""Compass""Thunder""Meadow""Sculpture""Tornado""Harbor""Chandelier"];
        
$array array_merge($array$extraArray);
    }

    
// Sorting
    
if (!empty($_POST['sort'])) {
        
$sortOrder $_POST['sort_order'] ?? 'asc';
        
$sortOrder === 'asc' sort($array) : rsort($array);
    }

    
// Reverse Order
    
if (!empty($_POST['reverse'])) {
        
$array array_reverse($array);
    }

    
// Remove Duplicates
    
if (!empty($_POST['unique'])) {
        
$array array_unique($array);
    }

    
// Shuffle
    
if (!empty($_POST['shuffle'])) {
        
shuffle($array);
    }

    
// Convert to Uppercase
    
if (!empty($_POST['uppercase'])) {
        
$array array_map('strtoupper'$array);
    }

    
// Searching for the first value in the array (if it exists)
    
$searchValue $array[0] ?? '';
    
$searchIndex $searchValue !== '' array_search($searchValue$array) : false;

    echo 
"<h3>Final Processed Array:</h3>";
    echo 
"<pre>" print_r($arraytrue) . "</pre>";

    echo 
"<h3>Search Result for '$searchValue':</h3>";
    echo 
$searchIndex !== false "Found at index: $searchIndex"Not found";
}
?>