Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $sequentialArray = array('apple', 'orange', 'tomato', 'carrot');
- $assocArray = array('fruit1' => 'apple',
- 'fruit2' => 'orange',
- 'veg1' => 'tomato',
- 'veg2' => 'carrot');
- <?php
- function isAssoc($arr)
- {
- return array_keys($arr) !== range(0, count($arr) - 1);
- }
- var_dump(isAssoc(array('a', 'b', 'c'))); // false
- var_dump(isAssoc(array("0" => 'a', "1" => 'b', "2" => 'c'))); // false
- var_dump(isAssoc(array("1" => 'a', "0" => 'b', "2" => 'c'))); // true
- var_dump(isAssoc(array("a" => 'a', "b" => 'b', "c" => 'c'))); // true
- ?>
- function is_assoc($array) {
- return (bool)count(array_filter(array_keys($array), 'is_string'));
- }
- <?php
- $arr = array(1,2,3,4);
- $isIndexed = array_values($arr) === $arr;
- for (reset($my_array); is_int(key($my_array)); next($my_array));
- $onlyIntKeys = is_null(key($my_array));
- function isAssoc($array)
- {
- return ($array !== array_values($array));
- }
- function isAssoc($array)
- {
- $array = array_keys($array); return ($array !== array_keys($array));
- }
- function checkAssoc($array){
- return ctype_digit( implode('', array_keys($array) ) );
- }
- function is_assoc($a){
- $a = array_keys($a);
- return ($a != array_keys($a));
- }
- function array_type( $obj ){
- $last_key = -1;
- $type = 'index';
- foreach( $obj as $key => $val ){
- if( !is_int( $key ) ){
- return 'assoc';
- }
- if( $key !== $last_key + 1 ){
- $type = 'sparse';
- }
- $last_key = $key;
- }
- return $type;
- }
- //! Check whether the input is an array whose keys are all integers.
- /*!
- param[in] $InputArray (array) Input array.
- return (bool) b true iff the input is an array whose keys are all integers.
- */
- function IsArrayAllKeyInt($InputArray)
- {
- if(!is_array($InputArray))
- {
- return false;
- }
- if(count($InputArray) <= 0)
- {
- return true;
- }
- return array_unique(array_map("is_int", array_keys($InputArray))) === array(true);
- }
- //! Check whether the input is an array whose keys are all strings.
- /*!
- param[in] $InputArray (array) Input array.
- return (bool) b true iff the input is an array whose keys are all strings.
- */
- function IsArrayAllKeyString($InputArray)
- {
- if(!is_array($InputArray))
- {
- return false;
- }
- if(count($InputArray) <= 0)
- {
- return true;
- }
- return array_unique(array_map("is_string", array_keys($InputArray))) === array(true);
- }
- //! Check whether the input is an array with at least one key being an integer and at least one key being a string.
- /*!
- param[in] $InputArray (array) Input array.
- return (bool) b true iff the input is an array with at least one key being an integer and at least one key being a string.
- */
- function IsArraySomeKeyIntAndSomeKeyString($InputArray)
- {
- if(!is_array($InputArray))
- {
- return false;
- }
- if(count($InputArray) <= 0)
- {
- return true;
- }
- return count(array_unique(array_map("is_string", array_keys($InputArray)))) >= 2;
- }
- //! Check whether the input is an array whose keys are numeric, sequential, and zero-based.
- /*!
- param[in] $InputArray (array) Input array.
- return (bool) b true iff the input is an array whose keys are numeric, sequential, and zero-based.
- */
- function IsArrayKeyNumericSequentialZeroBased($InputArray)
- {
- if(!is_array($InputArray))
- {
- return false;
- }
- if(count($InputArray) <= 0)
- {
- return true;
- }
- return array_keys($InputArray) === range(0, count($InputArray) - 1);
- }
- array(0 => "b");
- array(13 => "b");
- array(-13 => "b"); // Negative integers are also integers.
- array(0x1A => "b"); // Hexadecimal notation.
- array("fish and chips" => "b");
- array("" => "b"); // An empty string is also a string.
- array("stackoverflow_email@example.com" => "b"); // Strings may contain non-alphanumeric characters.
- array("stackt"over"rnflow's cool" => "b"); // Strings may contain special characters.
- array('$tα€k↔øv∈rflöw⛄' => "b"); // Strings may contain all kinds of symbols.
- array("functіon" => "b"); // You think this look fine? Think again! (see http://stackoverflow.com/q/9246051/1402846)
- array("ま말轉转ДŁ" => "b"); // How about Japanese/Korean/Chinese/Russian/Polish?
- array("fix0sh" => "b"); // Strings may contain null characters.
- array(file_get_contents("https://www.google.com.hk/images/nav_logo114.png") => "b"); // Strings may even be binary!
- array("13" => "b");
- array("-13" => "b"); // Negative, ok.
- array("13." => "b");
- array("+13" => "b"); // Positive, not ok.
- array("-013" => "b");
- array("0x1A" => "b"); // Not converted to integers even though it's a valid hexadecimal number.
- array("013" => "b"); // Not converted to integers even though it's a valid octal number.
- array("18446744073709551616" => "b"); // Not converted to integers as it can't fit into a 64-bit integer.
- array("60000000000" => "b"); // Array key could be integer or string, it can fit into a 64-bit (but not 32-bit) integer.
- array(1) {
- [2147483647]=>
- string(1) "b"
- }
- array(1) {
- ["2147483647"]=>
- string(1) "b"
- }
- function is_indexed_array(&$arr) {
- for (reset($arr); is_int(key($arr)); next($arr));
- return is_null(key($arr));
- }
- function is_sequential_array(&$arr, $base = 0) {
- for (reset($arr), $base = (int) $base; key($arr) === $base++; next($arr));
- return is_null(key($arr));
- }
- function is_asso($a){
- foreach(array_keys($a) as $key) {if (!is_int($key)) return TRUE;}
- return FALSE;
- }
- for i in 0 to len(your_array):
- if not defined(your-array[i]):
- # this is not an array array, it's an associative array :)
- function is_associative ( $a )
- {
- return in_array(false, array_map('is_numeric', array_keys($a)));
- }
- assert( true === is_associative(array(1, 2, 3, 4)) );
- assert( false === is_associative(array('foo' => 'bar', 'bar' => 'baz')) );
- assert( false === is_associative(array(1, 2, 3, 'foo' => 'bar')) );
- $a = array( 1, 2, 3, 4 );
- unset($a[1]);
- assert( true === is_associative($a) );
- <?php
- function is_list($array) {
- return array_keys($array) === range(0, count($array) - 1);
- }
- function is_assoc($array) {
- return count(array_filter(array_keys($array), 'is_string')) == count($array);
- }
- ?>
- function array_has_numeric_keys_only(array $array)
- {
- try {
- SplFixedArray::fromArray($array, true);
- } catch (InvalidArgumentException $e) {
- return false;
- }
- return true;
- }
- $arrays = Array(
- 'Array #1' => Array(1, 2, 3, 54, 23, 212, 123, 1, 1),
- 'Array #2' => Array("Stack", 1.5, 20, Array(3.4)),
- 'Array #3' => Array(1 => 4, 2 => 2),
- 'Array #4' => Array(3.0, "2", 3000, "Stack", 5 => "4"),
- 'Array #5' => Array("3" => 4, "2" => 2),
- 'Array #6' => Array("0" => "One", 1.0 => "Two", 2 => "Three"),
- 'Array #7' => Array(3 => "asdf", 4 => "asdf"),
- 'Array #8' => Array("apple" => 1, "orange" => 2),
- );
- function is_indexed_array_1(Array &$arr) {
- return $arr === array_values($arr);
- }
- function is_indexed_array_2(Array &$arr) {
- for (reset($arr), $i = 0; key($arr) === $i++; next($arr))
- ;
- return is_null(key($arr));
- }
- // Method #1
- $start = microtime(true);
- for ($i = 0; $i < 1000; $i++) {
- foreach ($arrays as $array) {
- $dummy = is_indexed_array_1($array);
- }
- }
- $end = microtime(true);
- echo "Time taken with method #1 = ".round(($end-$start)*1000.0,3)."msn";
- // Method #2
- $start = microtime(true);
- for ($i = 0; $i < 1000; $i++) {
- foreach ($arrays as $array) {
- $dummy = is_indexed_array_2($array);
- }
- }
- $end = microtime(true);
- echo "Time taken with method #1 = ".round(($end-$start)*1000.0,3)."msn";
- function is_associative($arr) {
- return (array_merge($arr) !== $arr || !is_numeric(implode(array_keys($arr))));
- }
- function isHash($array) {
- if (!is_array($array)) return false;
- $diff = array_diff_assoc($array, array_values($array));
- return (empty($diff)) ? false : true;
- }
- $n =count($arr);
- for($i=0,$i<$n;$i++)
- count() = 7 , max = 5, min=-1
- if( 7 == (5-(-1)+1 ) // true
- return false; // array not associative
- /**
- * isAssoc Checks if an array is associative
- * @param $arr reference to the array to be checked
- * @return bool
- */
- function IsAssoc(&$arr){
- $keys= array_keys($arr);
- foreach($keys as $key){
- if (!is_integer($key))
- return true;
- }
- // if all keys are integer then check if they are indexed
- if(count($arr) == (max($keys)-min($keys)+1))
- return false;
- else
- return true;
- }
- function is_array_assoc($foo) {
- if (is_array($foo)) {
- return (count(array_filter(array_keys($foo), 'is_string')) > 0);
- }
- return false;
- }
- isset($myArray[count($myArray) - 1])
- $myArray = array("1" => "apple", "b" => "banana");
- function arrayIsAssociative($myArray) {
- foreach (array_keys($myArray) as $ind => $key) {
- if (!is_numeric($key) || (isset($myArray[$ind + 1]) && $myArray[$ind + 1] != $key + 1)) {
- return true;
- }
- }
- return false;
- }
- // this will only return true if all the keys are numeric AND sequential, which
- // is what you get when you define an array like this:
- // array("a", "b", "c", "d", "e");
- function arrayIsAssociative($myArray) {
- $l = count($myArray);
- for ($i = 0; $i < $l, ++$i) {
- if (!isset($myArray[$i])) return true;
- }
- return false;
- }
- // this will return a false positive on an array like this:
- $x = array(1 => "b", 0 => "a", 2 => "c", 4 => "e", 3 => "d");
- public static function isArrayAssociative(array $array) {
- reset($array);
- $k = key($array);
- return !(is_int($k) || is_long($k));
- }
- <?php
- //$a is a subset of $b
- function isSubset($a, $b)
- {
- foreach($a =>$v)
- if(array_search($v, $b) === false)
- return false;
- return true;
- //less effecient, clearer implementation. (uses === for comparison)
- //return array_intersect($a, $b) === $a;
- }
- function isAssoc($arr)
- {
- return !isSubset(array_keys($arr), range(0, count($arr) - 1));
- }
- var_dump(isAssoc(array('a', 'b', 'c'))); // false
- var_dump(isAssoc(array(1 => 'a', 0 => 'b', 2 => 'c'))); // false
- var_dump(isAssoc(array("0" => 'a', "1" => 'b', "2" => 'c'))); // false
- //(use === in isSubset to get 'true' for above statement)
- var_dump(isAssoc(array("a" => 'a', "b" => 'b', "c" => 'c'))); // true
- ?>
- function isAssoc($arr = NULL)
- {
- if ($arr && is_array($arr))
- {
- foreach ($arr as $key => $val)
- {
- if (is_numeric($key)) { return true; }
- break;
- }
- }
- return false;
- }
- function Is_Indexed_Arr($arr){
- $arr_copy = $arr;
- if((2*count($arr)) == count(array_merge($arr, $arr_copy))){
- return 1;
- }
- return 0;
- }
- <?php
- function is_assoc($arr) { return (array_values($arr) !== $arr); }
- ?>
- private function is_hash($array) {
- foreach($array as $key => $value) {
- return ! is_int($key);
- }
- return false;
- }
- array_keys(array(
- "abc" => "gfb",
- "bdc" => "dbc"
- )
- );
- array(
- 0 => "abc",
- 1 => "bdc"
- )
- /**
- * Checks if an array is associative by utilizing REGEX against the keys
- * @param $arr <array> Reference to the array to be checked
- * @return boolean
- */
- private function isAssociativeArray( &$arr ) {
- return (bool)( preg_match( '/D/', implode( array_keys( $arr ) ) ) );
- }
- return array(
- "GetInventorySummary" => array(
- "Filters" => array(
- "Filter" => array(
- array(
- "FilterType" => "Shape",
- "FilterValue" => "W",
- ),
- array(
- "FilterType" => "Dimensions",
- "FilterValue" => "8 x 10",
- ),
- array(
- "FilterType" => "Grade",
- "FilterValue" => "A992",
- ),
- ),
- ),
- "SummaryField" => "Length",
- ),
- );
- return array(
- "GetInventorySummary" => array(
- "Filters" => array(
- "Filter" => array(
- "foo" => "bar",
- "bar" => "foo",
- ),
- ),
- "SummaryField" => "Length",
- ),
- );
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <GetInventorySummary>
- <Filters>
- <Filter>
- <FilterType>Shape</FilterType>
- <FilterValue>W</FilterValue>
- </Filter>
- <Filter>
- <FilterType>Dimensions</FilterType>
- <FilterValue>8 x 10</FilterValue>
- </Filter>
- <Filter>
- <FilterType>Grade</FilterType>
- <FilterValue>A992</FilterValue>
- </Filter>
- </Filters>
- <SummaryField>Length</SummaryField>
- </GetInventorySummary>
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <GetInventorySummary>
- <Filters>
- <Filter>
- <foo>bar</foo>
- <bar>foo</bar>
- </Filter>
- </Filters>
- <SummaryField>Length</SummaryField>
- </GetInventorySummary>
- function array_isassociative($array)
- {
- // Create new Array, Make it the same size as the input array
- $compareArray = array_pad(array(), count($array), 0);
- // Compare the two array_keys
- return (count(array_diff_key($array, $compareArray))) ? true : false;
- }
- <?php
- /**
- * Since PHP stores all arrays as associative internally, there is no proper
- * definition of a scalar array.
- *
- * As such, developers are likely to have varying definitions of scalar array,
- * based on their application needs.
- *
- * In this file, I present 3 increasingly strict methods of determining if an
- * array is scalar.
- *
- * @author David Farrell <DavidPFarrell@gmail.com>
- */
- /**
- * isArrayWithOnlyIntKeys defines a scalar array as containing
- * only integer keys.
- *
- * If you are explicitly setting integer keys on an array, you
- * may need this function to determine scalar-ness.
- *
- * @param array $a
- * @return boolean
- */
- function isArrayWithOnlyIntKeys(array $a)
- {
- if (!is_array($a))
- return false;
- foreach ($a as $k => $v)
- if (!is_int($k))
- return false;
- return true;
- }
- /**
- * isArrayWithOnlyAscendingIntKeys defines a scalar array as
- * containing only integer keys in ascending (but not necessarily
- * sequential) order.
- *
- * If you are performing pushes, pops, and unsets on your array,
- * you may need this function to determine scalar-ness.
- *
- * @param array $a
- * @return boolean
- */
- function isArrayWithOnlyAscendingIntKeys(array $a)
- {
- if (!is_array($a))
- return false;
- $prev = null;
- foreach ($a as $k => $v)
- {
- if (!is_int($k) || (null !== $prev && $k <= $prev))
- return false;
- $prev = $k;
- }
- return true;
- }
- /**
- * isArrayWithOnlyZeroBasedSequentialIntKeys defines a scalar array
- * as containing only integer keys in sequential, ascending order,
- * starting from 0.
- *
- * If you are only performing operations on your array that are
- * guaranteed to either maintain consistent key values, or that
- * re-base the keys for consistency, then you can use this function.
- *
- * @param array $a
- * @return boolean
- */
- function isArrayWithOnlyZeroBasedSequentialIntKeys(array $a)
- {
- if (!is_array($a))
- return false;
- $i = 0;
- foreach ($a as $k => $v)
- if ($i++ !== $k)
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement