What is the difference between an associative array and a numerical array in PHP?

An associative array in PHP uses named keys to store values, while a numerical array uses numeric indices. To differentiate between the two, you can check if the keys are numeric or not. If the keys are numeric, it's a numerical array; if the keys are strings, it's an associative array.

// Check if an array is associative or numerical
function isAssociativeArray($array) {
    return array_keys($array) !== range(0, count($array) - 1);
}

// Example usage
$array1 = array("name" => "John", "age" => 30);
$array2 = array("John", "Doe", "30");

if (isAssociativeArray($array1)) {
    echo "Array 1 is an associative array";
} else {
    echo "Array 1 is a numerical array";
}

if (isAssociativeArray($array2)) {
    echo "Array 2 is an associative array";
} else {
    echo "Array 2 is a numerical array";
}