Is array


The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot edu' are not accurate.

The functions fail to recognize indexes that are not in sequence or in order.  For example, array(0=>'a', 2=>'b', 1=>'c') and array(0=>'a', 3=>'b', 5=>'c') would be considered as sequential arrays. A true sequential array would be in consecutive order with no gaps in the indices.

The following solution utilizes the array_merge properties. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way.  The result must match the array passed to it in order to truly be a numerically indexed (sequential) array.  Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C).

The following functions will work for PHP >= 4.

<?php
   


an>implode( array_keys( $var ) ) ) );
    }
   
    function
is_assoc_array($var)
    {
        return (
array_merge($var) !== $var || !is_numeric( implode( array_keys( $var ) ) ) );
    }
?>

If you are not concerned about the actual order of the indices, you can change the comparison to == and != respectively.

php.net

All arrays in PHP are associative arrays, but it is quite easy to treat an associative array just like it is a sequential array. However, when dealing with XML-RPC, it is necessary to know whether an array is associative or sequential, so I created this function.
It isn't perfect, since an associative array that just happens to have sequential, integer keys starting with 0 will 'look' exactly like a sequential array, and will fool this function.
/****************************************************************
* is_assoc_array tries to decide whether or not a given array   *
* is an associative array, or a sequential array. Of course, no *
* such distinction is made by PHP, so it really just tests      *
* whether or not a given array could possibly be a sequential   *
* array. Since an associative array with sequential, integer    *
* keys 'looks' just like a sequential array, this function will *
* be fooled.                                                    *
*                                                               *
* BUG: Associative arrays with sequential, integer keys 'look'  *
* just like sequential arrays, and will be identified as such.


bsp;*
*                                                               *
****************************************************************/
function is_assoc_array( $php_val ) {
  if( !is_array( $php_val ) ){
     # Neither an associative, nor non-associative array.
     return false;
  }
  $given_keys = array_keys( $php_val );
  $non_assoc_keys = range( 0, count( $php_val ) );
  if( function_exists( 'array_diff_assoc' ) ) { # PHP > 4.3.0
     if( array_diff_assoc( $given_keys, $non_assoc_keys ) ){
        return true;
     }
     else {
        return false;
     }
  }
  else {
     if( array_diff( $given_keys, $non_assoc_keys ) and array_diff( $non_assoc_keys, $given_keys ) ){
        return true;
     }
     else {
        return false;
     }
  }
}

www.navioo.com


The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot edu' are not accurate.

The functions fail to recognize indexes that are not in sequence or in order.  For example, array(0=>'a', 2=>'b', 1=>'c') and array(0=>'a', 3=>'b', 5=>'c') would be considered as sequential arrays. A true sequential array would be in consecutive order with no gaps in the indices.

The following solution utilizes the array_merge properties. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way.  The result must match the array passed to it in order to truly be a numerically indexed (sequential) array.  Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C).

The following functions will work for PHP >= 4.

<?php
   
function <.


fault">implode
( array_keys( $var ) ) ) );
    }
   
    function
is_assoc_array($var)
    {
        return (
array_merge($var) !== $var || !is_numeric( implode( array_keys( $var ) ) ) );
    }
?>

If you are not concerned about the actual order of the indices, you can change the comparison to == and != respectively.

mirrors.segmentfault.com

All arrays in PHP are associative arrays, but it is quite easy to treat an associative array just like it is a sequential array. However, when dealing with XML-RPC, it is necessary to know whether an array is associative or sequential, so I created this function.

It isn't perfect, since an associative array that just happens to have sequential, integer keys starting with 0 will 'look' exactly like a sequential array, and will fool this function.

/****************************************************************
* is_assoc_array tries to decide whether or not a given array  *
* is an associative array, or a sequential array. Of course, no *
* such distinction is made by PHP, so it really just tests      *
* whether or not a given array could possibly be a sequential  *
* array. Since an associative array with sequential, integer    *
* keys 'looks' just like a sequential array, this function will *
* be fooled.                                                &n.


nbsp;                                 *
****************************************************************/
function is_assoc_array( $php_val ) {
   if( !is_array( $php_val ) ){
     # Neither an associative, nor non-associative array.
     return false;
   }

   $given_keys = array_keys( $php_val );
   $non_assoc_keys = range( 0, count( $php_val ) );

   if( function_exists( 'array_diff_assoc' ) ) { # PHP > 4.3.0
     if( array_diff_assoc( $given_keys, $non_assoc_keys ) ){
         return true;
     }
     else {
         return false;
     }
   }
   else {
     if( array_diff( $given_keys, $non_assoc_keys ) and array_diff( $non_assoc_keys, $given_keys ) ){
         return true;
     }
     else {
         return false;
     }
   }
}

php.svchat.ru


You May Also Like

About the Author: admind

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.