Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.51 KB | None | 0 0
  1. $sequentialArray = array('apple', 'orange', 'tomato', 'carrot');
  2.  
  3. $assocArray = array('fruit1' => 'apple',
  4. 'fruit2' => 'orange',
  5. 'veg1' => 'tomato',
  6. 'veg2' => 'carrot');
  7.  
  8. <?php
  9.  
  10. function isAssoc($arr)
  11. {
  12. return array_keys($arr) !== range(0, count($arr) - 1);
  13. }
  14.  
  15. var_dump(isAssoc(array('a', 'b', 'c'))); // false
  16. var_dump(isAssoc(array("0" => 'a', "1" => 'b', "2" => 'c'))); // false
  17. var_dump(isAssoc(array("1" => 'a', "0" => 'b', "2" => 'c'))); // true
  18. var_dump(isAssoc(array("a" => 'a', "b" => 'b', "c" => 'c'))); // true
  19.  
  20. ?>
  21.  
  22. function is_assoc($array) {
  23. return (bool)count(array_filter(array_keys($array), 'is_string'));
  24. }
  25.  
  26. <?php
  27. $arr = array(1,2,3,4);
  28. $isIndexed = array_values($arr) === $arr;
  29.  
  30. for (reset($my_array); is_int(key($my_array)); next($my_array));
  31. $onlyIntKeys = is_null(key($my_array));
  32.  
  33. function isAssoc($array)
  34. {
  35. return ($array !== array_values($array));
  36. }
  37.  
  38. function isAssoc($array)
  39. {
  40. $array = array_keys($array); return ($array !== array_keys($array));
  41. }
  42.  
  43. function checkAssoc($array){
  44. return ctype_digit( implode('', array_keys($array) ) );
  45. }
  46.  
  47. function is_assoc($a){
  48. $a = array_keys($a);
  49. return ($a != array_keys($a));
  50. }
  51.  
  52. function array_type( $obj ){
  53. $last_key = -1;
  54. $type = 'index';
  55. foreach( $obj as $key => $val ){
  56. if( !is_int( $key ) ){
  57. return 'assoc';
  58. }
  59. if( $key !== $last_key + 1 ){
  60. $type = 'sparse';
  61. }
  62. $last_key = $key;
  63. }
  64. return $type;
  65. }
  66.  
  67. //! Check whether the input is an array whose keys are all integers.
  68. /*!
  69. param[in] $InputArray (array) Input array.
  70. return (bool) b true iff the input is an array whose keys are all integers.
  71. */
  72. function IsArrayAllKeyInt($InputArray)
  73. {
  74. if(!is_array($InputArray))
  75. {
  76. return false;
  77. }
  78.  
  79. if(count($InputArray) <= 0)
  80. {
  81. return true;
  82. }
  83.  
  84. return array_unique(array_map("is_int", array_keys($InputArray))) === array(true);
  85. }
  86.  
  87. //! Check whether the input is an array whose keys are all strings.
  88. /*!
  89. param[in] $InputArray (array) Input array.
  90. return (bool) b true iff the input is an array whose keys are all strings.
  91. */
  92. function IsArrayAllKeyString($InputArray)
  93. {
  94. if(!is_array($InputArray))
  95. {
  96. return false;
  97. }
  98.  
  99. if(count($InputArray) <= 0)
  100. {
  101. return true;
  102. }
  103.  
  104. return array_unique(array_map("is_string", array_keys($InputArray))) === array(true);
  105. }
  106.  
  107. //! Check whether the input is an array with at least one key being an integer and at least one key being a string.
  108. /*!
  109. param[in] $InputArray (array) Input array.
  110. 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.
  111. */
  112. function IsArraySomeKeyIntAndSomeKeyString($InputArray)
  113. {
  114. if(!is_array($InputArray))
  115. {
  116. return false;
  117. }
  118.  
  119. if(count($InputArray) <= 0)
  120. {
  121. return true;
  122. }
  123.  
  124. return count(array_unique(array_map("is_string", array_keys($InputArray)))) >= 2;
  125. }
  126.  
  127. //! Check whether the input is an array whose keys are numeric, sequential, and zero-based.
  128. /*!
  129. param[in] $InputArray (array) Input array.
  130. return (bool) b true iff the input is an array whose keys are numeric, sequential, and zero-based.
  131. */
  132. function IsArrayKeyNumericSequentialZeroBased($InputArray)
  133. {
  134. if(!is_array($InputArray))
  135. {
  136. return false;
  137. }
  138.  
  139. if(count($InputArray) <= 0)
  140. {
  141. return true;
  142. }
  143.  
  144. return array_keys($InputArray) === range(0, count($InputArray) - 1);
  145. }
  146.  
  147. array(0 => "b");
  148. array(13 => "b");
  149. array(-13 => "b"); // Negative integers are also integers.
  150. array(0x1A => "b"); // Hexadecimal notation.
  151.  
  152. array("fish and chips" => "b");
  153. array("" => "b"); // An empty string is also a string.
  154. array("stackoverflow_email@example.com" => "b"); // Strings may contain non-alphanumeric characters.
  155. array("stackt"over"rnflow's cool" => "b"); // Strings may contain special characters.
  156. array('$tα€k↔øv∈rflöw⛄' => "b"); // Strings may contain all kinds of symbols.
  157. array("functіon" => "b"); // You think this look fine? Think again! (see http://stackoverflow.com/q/9246051/1402846)
  158. array("ま말轉转ДŁ" => "b"); // How about Japanese/Korean/Chinese/Russian/Polish?
  159. array("fix0sh" => "b"); // Strings may contain null characters.
  160. array(file_get_contents("https://www.google.com.hk/images/nav_logo114.png") => "b"); // Strings may even be binary!
  161.  
  162. array("13" => "b");
  163. array("-13" => "b"); // Negative, ok.
  164.  
  165. array("13." => "b");
  166. array("+13" => "b"); // Positive, not ok.
  167. array("-013" => "b");
  168. array("0x1A" => "b"); // Not converted to integers even though it's a valid hexadecimal number.
  169. array("013" => "b"); // Not converted to integers even though it's a valid octal number.
  170. array("18446744073709551616" => "b"); // Not converted to integers as it can't fit into a 64-bit integer.
  171.  
  172. array("60000000000" => "b"); // Array key could be integer or string, it can fit into a 64-bit (but not 32-bit) integer.
  173.  
  174. array(1) {
  175. [2147483647]=>
  176. string(1) "b"
  177. }
  178.  
  179. array(1) {
  180. ["2147483647"]=>
  181. string(1) "b"
  182. }
  183.  
  184. function is_indexed_array(&$arr) {
  185. for (reset($arr); is_int(key($arr)); next($arr));
  186. return is_null(key($arr));
  187. }
  188.  
  189. function is_sequential_array(&$arr, $base = 0) {
  190. for (reset($arr), $base = (int) $base; key($arr) === $base++; next($arr));
  191. return is_null(key($arr));
  192. }
  193.  
  194. function is_asso($a){
  195. foreach(array_keys($a) as $key) {if (!is_int($key)) return TRUE;}
  196. return FALSE;
  197. }
  198.  
  199. for i in 0 to len(your_array):
  200. if not defined(your-array[i]):
  201. # this is not an array array, it's an associative array :)
  202.  
  203. function is_associative ( $a )
  204. {
  205. return in_array(false, array_map('is_numeric', array_keys($a)));
  206. }
  207.  
  208. assert( true === is_associative(array(1, 2, 3, 4)) );
  209.  
  210. assert( false === is_associative(array('foo' => 'bar', 'bar' => 'baz')) );
  211.  
  212. assert( false === is_associative(array(1, 2, 3, 'foo' => 'bar')) );
  213.  
  214. $a = array( 1, 2, 3, 4 );
  215.  
  216. unset($a[1]);
  217.  
  218. assert( true === is_associative($a) );
  219.  
  220. <?php
  221.  
  222. function is_list($array) {
  223. return array_keys($array) === range(0, count($array) - 1);
  224. }
  225.  
  226. function is_assoc($array) {
  227. return count(array_filter(array_keys($array), 'is_string')) == count($array);
  228. }
  229.  
  230. ?>
  231.  
  232. function array_has_numeric_keys_only(array $array)
  233. {
  234. try {
  235. SplFixedArray::fromArray($array, true);
  236. } catch (InvalidArgumentException $e) {
  237. return false;
  238. }
  239. return true;
  240. }
  241.  
  242. $arrays = Array(
  243. 'Array #1' => Array(1, 2, 3, 54, 23, 212, 123, 1, 1),
  244. 'Array #2' => Array("Stack", 1.5, 20, Array(3.4)),
  245. 'Array #3' => Array(1 => 4, 2 => 2),
  246. 'Array #4' => Array(3.0, "2", 3000, "Stack", 5 => "4"),
  247. 'Array #5' => Array("3" => 4, "2" => 2),
  248. 'Array #6' => Array("0" => "One", 1.0 => "Two", 2 => "Three"),
  249. 'Array #7' => Array(3 => "asdf", 4 => "asdf"),
  250. 'Array #8' => Array("apple" => 1, "orange" => 2),
  251. );
  252.  
  253. function is_indexed_array_1(Array &$arr) {
  254. return $arr === array_values($arr);
  255. }
  256.  
  257. function is_indexed_array_2(Array &$arr) {
  258. for (reset($arr), $i = 0; key($arr) === $i++; next($arr))
  259. ;
  260. return is_null(key($arr));
  261. }
  262.  
  263. // Method #1
  264. $start = microtime(true);
  265. for ($i = 0; $i < 1000; $i++) {
  266. foreach ($arrays as $array) {
  267. $dummy = is_indexed_array_1($array);
  268. }
  269. }
  270. $end = microtime(true);
  271. echo "Time taken with method #1 = ".round(($end-$start)*1000.0,3)."msn";
  272.  
  273. // Method #2
  274. $start = microtime(true);
  275. for ($i = 0; $i < 1000; $i++) {
  276. foreach ($arrays as $array) {
  277. $dummy = is_indexed_array_2($array);
  278. }
  279. }
  280. $end = microtime(true);
  281. echo "Time taken with method #1 = ".round(($end-$start)*1000.0,3)."msn";
  282.  
  283. function is_associative($arr) {
  284. return (array_merge($arr) !== $arr || !is_numeric(implode(array_keys($arr))));
  285. }
  286.  
  287. function isHash($array) {
  288. if (!is_array($array)) return false;
  289. $diff = array_diff_assoc($array, array_values($array));
  290. return (empty($diff)) ? false : true;
  291. }
  292.  
  293. $n =count($arr);
  294. for($i=0,$i<$n;$i++)
  295.  
  296. count() = 7 , max = 5, min=-1
  297.  
  298.  
  299.  
  300. if( 7 == (5-(-1)+1 ) // true
  301. return false; // array not associative
  302.  
  303.  
  304. /**
  305. * isAssoc Checks if an array is associative
  306. * @param $arr reference to the array to be checked
  307. * @return bool
  308. */
  309. function IsAssoc(&$arr){
  310. $keys= array_keys($arr);
  311. foreach($keys as $key){
  312. if (!is_integer($key))
  313. return true;
  314. }
  315. // if all keys are integer then check if they are indexed
  316. if(count($arr) == (max($keys)-min($keys)+1))
  317. return false;
  318. else
  319. return true;
  320. }
  321.  
  322. function is_array_assoc($foo) {
  323. if (is_array($foo)) {
  324. return (count(array_filter(array_keys($foo), 'is_string')) > 0);
  325. }
  326. return false;
  327. }
  328.  
  329. isset($myArray[count($myArray) - 1])
  330.  
  331. $myArray = array("1" => "apple", "b" => "banana");
  332.  
  333. function arrayIsAssociative($myArray) {
  334. foreach (array_keys($myArray) as $ind => $key) {
  335. if (!is_numeric($key) || (isset($myArray[$ind + 1]) && $myArray[$ind + 1] != $key + 1)) {
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. // this will only return true if all the keys are numeric AND sequential, which
  342. // is what you get when you define an array like this:
  343. // array("a", "b", "c", "d", "e");
  344.  
  345. function arrayIsAssociative($myArray) {
  346. $l = count($myArray);
  347. for ($i = 0; $i < $l, ++$i) {
  348. if (!isset($myArray[$i])) return true;
  349. }
  350. return false;
  351. }
  352. // this will return a false positive on an array like this:
  353. $x = array(1 => "b", 0 => "a", 2 => "c", 4 => "e", 3 => "d");
  354.  
  355. public static function isArrayAssociative(array $array) {
  356. reset($array);
  357. $k = key($array);
  358. return !(is_int($k) || is_long($k));
  359. }
  360.  
  361. <?php
  362. //$a is a subset of $b
  363. function isSubset($a, $b)
  364. {
  365. foreach($a =>$v)
  366. if(array_search($v, $b) === false)
  367. return false;
  368.  
  369. return true;
  370.  
  371. //less effecient, clearer implementation. (uses === for comparison)
  372. //return array_intersect($a, $b) === $a;
  373. }
  374.  
  375. function isAssoc($arr)
  376. {
  377. return !isSubset(array_keys($arr), range(0, count($arr) - 1));
  378. }
  379.  
  380. var_dump(isAssoc(array('a', 'b', 'c'))); // false
  381. var_dump(isAssoc(array(1 => 'a', 0 => 'b', 2 => 'c'))); // false
  382. var_dump(isAssoc(array("0" => 'a', "1" => 'b', "2" => 'c'))); // false
  383. //(use === in isSubset to get 'true' for above statement)
  384. var_dump(isAssoc(array("a" => 'a', "b" => 'b', "c" => 'c'))); // true
  385. ?>
  386.  
  387. function isAssoc($arr = NULL)
  388. {
  389. if ($arr && is_array($arr))
  390. {
  391. foreach ($arr as $key => $val)
  392. {
  393. if (is_numeric($key)) { return true; }
  394.  
  395. break;
  396. }
  397. }
  398.  
  399. return false;
  400. }
  401.  
  402. function Is_Indexed_Arr($arr){
  403. $arr_copy = $arr;
  404. if((2*count($arr)) == count(array_merge($arr, $arr_copy))){
  405. return 1;
  406. }
  407. return 0;
  408. }
  409.  
  410. <?php
  411. function is_assoc($arr) { return (array_values($arr) !== $arr); }
  412. ?>
  413.  
  414. private function is_hash($array) {
  415. foreach($array as $key => $value) {
  416. return ! is_int($key);
  417. }
  418. return false;
  419. }
  420.  
  421. array_keys(array(
  422. "abc" => "gfb",
  423. "bdc" => "dbc"
  424. )
  425. );
  426.  
  427. array(
  428. 0 => "abc",
  429. 1 => "bdc"
  430. )
  431.  
  432. /**
  433. * Checks if an array is associative by utilizing REGEX against the keys
  434. * @param $arr <array> Reference to the array to be checked
  435. * @return boolean
  436. */
  437. private function isAssociativeArray( &$arr ) {
  438. return (bool)( preg_match( '/D/', implode( array_keys( $arr ) ) ) );
  439. }
  440.  
  441. return array(
  442. "GetInventorySummary" => array(
  443. "Filters" => array(
  444. "Filter" => array(
  445. array(
  446. "FilterType" => "Shape",
  447. "FilterValue" => "W",
  448. ),
  449. array(
  450. "FilterType" => "Dimensions",
  451. "FilterValue" => "8 x 10",
  452. ),
  453. array(
  454. "FilterType" => "Grade",
  455. "FilterValue" => "A992",
  456. ),
  457. ),
  458. ),
  459. "SummaryField" => "Length",
  460. ),
  461. );
  462.  
  463. return array(
  464. "GetInventorySummary" => array(
  465. "Filters" => array(
  466. "Filter" => array(
  467. "foo" => "bar",
  468. "bar" => "foo",
  469. ),
  470. ),
  471. "SummaryField" => "Length",
  472. ),
  473. );
  474.  
  475. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  476. <GetInventorySummary>
  477. <Filters>
  478. <Filter>
  479. <FilterType>Shape</FilterType>
  480. <FilterValue>W</FilterValue>
  481. </Filter>
  482. <Filter>
  483. <FilterType>Dimensions</FilterType>
  484. <FilterValue>8 x 10</FilterValue>
  485. </Filter>
  486. <Filter>
  487. <FilterType>Grade</FilterType>
  488. <FilterValue>A992</FilterValue>
  489. </Filter>
  490. </Filters>
  491. <SummaryField>Length</SummaryField>
  492. </GetInventorySummary>
  493.  
  494. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  495. <GetInventorySummary>
  496. <Filters>
  497. <Filter>
  498. <foo>bar</foo>
  499. <bar>foo</bar>
  500. </Filter>
  501. </Filters>
  502. <SummaryField>Length</SummaryField>
  503. </GetInventorySummary>
  504.  
  505. function array_isassociative($array)
  506. {
  507. // Create new Array, Make it the same size as the input array
  508. $compareArray = array_pad(array(), count($array), 0);
  509.  
  510. // Compare the two array_keys
  511. return (count(array_diff_key($array, $compareArray))) ? true : false;
  512.  
  513. }
  514.  
  515. <?php
  516. /**
  517. * Since PHP stores all arrays as associative internally, there is no proper
  518. * definition of a scalar array.
  519. *
  520. * As such, developers are likely to have varying definitions of scalar array,
  521. * based on their application needs.
  522. *
  523. * In this file, I present 3 increasingly strict methods of determining if an
  524. * array is scalar.
  525. *
  526. * @author David Farrell <DavidPFarrell@gmail.com>
  527. */
  528.  
  529. /**
  530. * isArrayWithOnlyIntKeys defines a scalar array as containing
  531. * only integer keys.
  532. *
  533. * If you are explicitly setting integer keys on an array, you
  534. * may need this function to determine scalar-ness.
  535. *
  536. * @param array $a
  537. * @return boolean
  538. */
  539. function isArrayWithOnlyIntKeys(array $a)
  540. {
  541. if (!is_array($a))
  542. return false;
  543. foreach ($a as $k => $v)
  544. if (!is_int($k))
  545. return false;
  546. return true;
  547. }
  548.  
  549. /**
  550. * isArrayWithOnlyAscendingIntKeys defines a scalar array as
  551. * containing only integer keys in ascending (but not necessarily
  552. * sequential) order.
  553. *
  554. * If you are performing pushes, pops, and unsets on your array,
  555. * you may need this function to determine scalar-ness.
  556. *
  557. * @param array $a
  558. * @return boolean
  559. */
  560. function isArrayWithOnlyAscendingIntKeys(array $a)
  561. {
  562. if (!is_array($a))
  563. return false;
  564. $prev = null;
  565. foreach ($a as $k => $v)
  566. {
  567. if (!is_int($k) || (null !== $prev && $k <= $prev))
  568. return false;
  569. $prev = $k;
  570. }
  571. return true;
  572. }
  573.  
  574. /**
  575. * isArrayWithOnlyZeroBasedSequentialIntKeys defines a scalar array
  576. * as containing only integer keys in sequential, ascending order,
  577. * starting from 0.
  578. *
  579. * If you are only performing operations on your array that are
  580. * guaranteed to either maintain consistent key values, or that
  581. * re-base the keys for consistency, then you can use this function.
  582. *
  583. * @param array $a
  584. * @return boolean
  585. */
  586. function isArrayWithOnlyZeroBasedSequentialIntKeys(array $a)
  587. {
  588. if (!is_array($a))
  589. return false;
  590. $i = 0;
  591. foreach ($a as $k => $v)
  592. if ($i++ !== $k)
  593. return false;
  594. return true;
  595. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement