Avatar ·

How to check if an array in PHP is associative or sequential (indexed)?

Hello everyone! I just started learning PHP and encountered a confusing situation. I know that PHP has two types of arrays: regular (sequential) with numeric keys starting from 0, and associative where keys are strings. But in reality, everything is mixed: an array can start with key 0, then have a string, and then a number again. Or keys can be numbers but not in order (e.g., 0, 5, 10). I need to write a function that processes data from a form. If the array is sequential (i.e., keys go in order: 0, 1, 2, 3...), I apply one logic. If it's associative (e.g., keys 'name', 'email', 'age') — another logic. But the standard check using `array_keys()` doesn't give a clear answer, because an array like `[0 => 'a', 2 => 'b']` is formally numeric but not sequential. Here's an example: