Skip to content

Commit d5123cf

Browse files
committed
Improve is_assoc function
Signed-off-by: Kirtan Gajjar <[email protected]>
1 parent 4c847a5 commit d5123cf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

php/utils.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,23 @@ function make_progress_bar( $message, $count, $interval = 100 ) {
424424

425425
/**
426426
* Checks if an array is associative array
427+
*
427428
* @param array $arr array to check
428429
*
429430
* @return bool
430431
*/
431-
function is_assoc( array $arr ) {
432-
if ( array() === $arr ) {
433-
return false;
432+
function is_assoc( $arr ) {
433+
434+
$is_assoc = false;
435+
436+
foreach ( $arr as $key => $value ) {
437+
if ( is_string( $key ) ) {
438+
$is_assoc = true;
439+
break;
440+
}
434441
}
435442

436-
return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
443+
return $is_assoc;
437444
}
438445

439446
function parse_url( $url ) {

0 commit comments

Comments
 (0)