/php/array_tools.php

https://github.com/jtrick/ACE · PHP · 105 lines · 64 code · 21 blank · 20 comment · 20 complexity · a6a1337a0b9656a822f6e9caada676df MD5 · raw file

  1. <?php // Copyright (c) 2003-2009 Jeff Trickett, all rights reserved. License agreement available at http://jefftrickett.com/license/?Id=Tools
  2. // Counts the total depth of an array.
  3. function CountArrayDepth($MainArray) {
  4. if (!count($MainArray)) { return 0; }
  5. $HighCount = 0;
  6. for ($ThisRow=0;$ThisArray=$MainArray[$ThisRow];$ThisRow++) {
  7. if (is_array($ThisArray)) {
  8. $ThisCount = CountArrayDepth($MainArray);
  9. if ($ThisCount > $HighCount) { $HighCount = $ThisCount; }
  10. }
  11. }
  12. return ($HighCount + 1); // To take into account the depth of $MainArray itself.
  13. }
  14. // Checks to see if two arrays match. Returns 1 if they do, 0 otherwise.
  15. function CheckArrayMatch($FirstArray, $NextArray) {
  16. if ($FirstArray && $NextArray) {
  17. if (is_array($FirstArray) && is_array($NextArray)) {
  18. $DiffArray = array_diff($FirstArray, $NextArray);
  19. if (count($DiffArray) < 1) { return 1; }
  20. }
  21. }
  22. }
  23. // Checks to see whether an array is associative or not.
  24. function CheckAssocArray($Array, $Echo=0) {
  25. if (!is_array($Array) || empty($Array)) { return; }
  26. $Keys = array_keys($Array);
  27. $Result = (array_keys($Keys) !== $Keys);
  28. if ($Echo) { EchoV(array('$Array'=>$Array), "\$Array " . (($Result) ? ('IS') : ('is NOT')) . " an associative array."); }
  29. return $Result;
  30. }
  31. // The following were taken directly from a php.net posting:
  32. function is_assoc($array) { return is_array($array) && count($array) !== array_reduce(array_keys($array), 'is_assoc_callback', 0); }
  33. function is_assoc_callback($a, $b) { return $a === $b ? $a + 1 : 0; }
  34. // Breaks a string in csv format into an array of subarrays. $Delimiter defaults to "\t", $RefArray are the column headings to be used to separate the subarrays, if desired. $StringDelimeter will chop string quotes for array strings if (!= -1). $KeepEmptyRows will retain empty rows rather than dropping them.
  35. function CsvToArray($String, $Delimiter="\t", $KeyArray=0, $StringDelimeter=0, $KeepEmptyRows=0) {
  36. //echo "<p>array_tools.php CsvToArray() \$String: |{$String}|</p>\n";
  37. if (!$StringDelimeter) { $StringDelimeter = '"'; } // Fix?
  38. $RowArray = StringRowArray($String);
  39. //echo "<p>array_tools.php CsvToArray() \$RowArray: ".HtmlArray($RowArray)."</p>\n";
  40. foreach($RowArray as $ThisRowString) {
  41. if ($ThisRowString) {
  42. //echo "<p>array_tools.php CsvToArray() \$ThisRowString: |{$ThisRowString}|</p>\n";
  43. $ThisLineArray = explode($Delimiter, $ThisRowString);
  44. //echo "<p>array_tools.php CsvToArray() \$ThisLineArray: ".HtmlArray($ThisLineArray)."</p>\n";
  45. if ($StringDelimeter != -1) {
  46. $StrippedArray = array();
  47. foreach($ThisLineArray as $ThisItem) { $StrippedArray[] = trim($ThisItem, $StringDelimeter); }
  48. //echo "<p>array_tools.php CsvToArray() \$ThisLineArray: ".HtmlArray($ThisLineArray).", \$StrippedArray: ".HtmlArray($StrippedArray)."</p>\n";
  49. $ThisLineArray = $StrippedArray;
  50. }
  51. $StringArray[] = MergeArraysToAssoc($KeyArray, $ThisLineArray);
  52. } else if ($KeepEmptyRows) {
  53. $StringArray[] = MergeArraysToAssoc($KeyArray);
  54. }
  55. }
  56. //echo "<p>array_tools.php CsvToArray() \$StringArray: ".HtmlArray($StringArray)."</p>\n";
  57. return $StringArray;
  58. }
  59. // Merges two simple arrays into an associative array. If $CutOff, the arrays will be rerstricted to the number of fields in $KeyArray, otherwise they will continue past as numerical keys.
  60. function MergeArraysToAssoc($KeyArray, $ValArray=0, $CutOff=0) { // Fix!! Make breakproof, decide on behavior.
  61. //echo "<p>array_tools.php MergeArraysToAssoc() \$KeyArray: ".HtmlArray($KeyArray).", \$ValArray: ".HtmlArray($ValArray).", \$CutOff: |{$CutOff}|</p>\n";
  62. foreach($KeyArray as $ThisNum=>$ThisField) {
  63. $ThisVal = $ValArray[$ThisNum];
  64. $NewArray[$ThisField] = $ThisVal;
  65. }
  66. //echo "<p>array_tools.php MergeArraysToAssoc() \$NewArray: ".HtmlArray($NewArray)."</p>\n";
  67. return $NewArray;
  68. }
  69. // Breaks a string into rows and returns it as an array of the rows.
  70. function StringRowArray($String) {
  71. //echo "<p>array_tools.php StringRowArray() \$String: |{$String}|</p>\n";
  72. //$NewString = str_replace(array("\r\n","\n\r","\r"),"\n", $String); // Fix? to bring file down to normal '\n' format.
  73. $NewString = $String; // Fix! Make the above process work correctly.
  74. //echo "<p>array_tools.php StringRowArray() \$NewString: |{$NewString}|</p>\n";
  75. $RowArray = explode("\n", $NewString);
  76. //echo "<p>array_tools.php StringRowArray() \$RowArray: ".HtmlArray($RowArray)."</p>\n";
  77. return $RowArray;
  78. }
  79. // Outputs an array in table form.
  80. function HTMLTableArray($MainArray) {
  81. if (!count($MainArray)) { return "Array is Empty!"; }
  82. $TableColTotal = CountArrayDepth($MainArray);
  83. }
  84. ?>