PHP Check for Numeric/Associative Array

<?php // numeric: ['foo', 'bar', 'baz', 3 => 'qux'] // associative: ['foo' => 'bar', 'baz' => 'qux'] ### METHOD 1 return strpos(json_encode($array), '[') === 0; // is numeric return strpos(json_encode($array), '{') === 0; // is associative ### METHOD 2 return array_keys($array) === range(0, count($array) - 1); // is numeric return array_keys($array) !== range(0, count($array) - 1); // is associative

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.