Guest User

Untitled

a guest
Jun 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. for(int i=0; i< arr.length;i++){
  2.  
  3. boolean isLastElem = i== (arr.length -1) ? true : false;
  4.  
  5. }
  6.  
  7. $numItems = count($arr);
  8. $i = 0;
  9. foreach($arr as $key=>$value) {
  10. if($i+1 == $numItems) {
  11. echo "last index!";
  12. }
  13. $i++;
  14. }
  15.  
  16. $last_key = end(array_keys($array));
  17. foreach ($array as $key => $value) {
  18. if ($key == $last_key) {
  19. // last element
  20. } else {
  21. // not last element
  22. }
  23. }
  24.  
  25. foreach($input as $key => $value) {
  26. $ret .= "$value";
  27. if (next($input)==true) $ret .= ",";
  28. }
  29.  
  30. $toEnd = count($arr);
  31. foreach($arr as $key=>$value) {
  32. if (--$toEnd) {
  33. echo "last index! $value";
  34. }
  35. }
  36.  
  37. foreach($arr as $key=>$value) {
  38. //something
  39. }
  40. echo "last index! $key => $value";
  41.  
  42. $keys = array_keys($array);
  43. for ($i = 0, $l = count($array); $i < $l; ++$i) {
  44. $key = $array[$i];
  45. $value = $array[$key];
  46. $isLastItem = ($i == ($l - 1));
  47. // do stuff
  48. }
  49.  
  50. // or this way...
  51.  
  52. $i = 0;
  53. $l = count($array);
  54. foreach ($array as $key => $value) {
  55. $isLastItem = ($i == ($l - 1));
  56. // do stuff
  57. ++$i;
  58. }
  59.  
  60. for ($i=0;$i<count(arr);$i++){
  61. $i == count(arr)-1 ? true : false;
  62. }
  63.  
  64. end(arr);
  65.  
  66. arr[1];
  67.  
  68. $first = true;
  69. foreach($array as $key => $value) {
  70. if ($first) {
  71. $first = false;
  72. // Do what you want to do before the first element
  73. echo "List of key, value pairs:n";
  74. } else {
  75. // Do what you want to do at the end of every element
  76. // except the last, assuming the list has more than one element
  77. echo "n";
  78. }
  79. // Do what you want to do for the current element
  80. echo $key . ' => ' . $value;
  81. }
  82.  
  83. $first = true;
  84. foreach($array as $key => $subArray) {
  85. if ($first) {
  86. $string = "List of key => value array pairs:n";
  87. $first = false;
  88. } else {
  89. echo "n";
  90. }
  91.  
  92. $string .= $key . '=>(';
  93. $first = true;
  94. foreach($subArray as $key => $value) {
  95. if ($first) {
  96. $first = false;
  97. } else {
  98. $string .= ', ';
  99. }
  100. $string .= $key . '=>' . $value;
  101. }
  102. $string .= ')';
  103. }
  104. echo $string;
  105.  
  106. List of key => value array pairs:
  107. key1=>(v1_key1=>v1_val1, v1_key2=>v1_val2)
  108. key2=>(v2_key1=>v2_val1, v2_key2=>v2_val2, v2_key3=>v2_val3)
  109. key3=>(v3_key1=>v3_val1)
  110.  
  111. $arr = range(1, 10);
  112.  
  113. $end = end($arr);
  114. reset($arr);
  115.  
  116. while( list($k, $v) = each($arr) )
  117. {
  118. if( $n == $end )
  119. {
  120. echo 'last!';
  121. }
  122. else
  123. {
  124. echo sprintf('%s ', $v);
  125. }
  126. }
  127.  
  128. $rev_array = array_reverse($array);
  129.  
  130. echo array_pop($rev_array);
  131.  
  132. <?php
  133. $week=array('one'=>'monday','two'=>'tuesday','three'=>'wednesday','four'=>'thursday','five'=>'friday','six'=>'saturday','seven'=>'sunday');
  134. $keys = array_keys($week);
  135. $string = "INSERT INTO my_table ('";
  136. $string .= implode("','", $keys);
  137. $string .= "') VALUES ('";
  138. $string .= implode("','", $week);
  139. $string .= "');";
  140. echo $string;
  141. ?>
  142.  
  143. end( $elements );
  144. $endKey = key($elements);
  145. foreach ($elements as $key => $value)
  146. {
  147. if ($key == $endKey) // -- this is the last item
  148. {
  149. // do something
  150. }
  151.  
  152. // more code
  153. }
  154.  
  155. $numItems = count($arr);
  156. $i=0;
  157. $firstitem=$arr[0];
  158. $i++;
  159. while($i<$numItems-1){
  160. $some_item=$arr[$i];
  161. $i++;
  162. }
  163. $last_item=$arr[$i];
  164. $i++;
  165.  
  166. $array = array(
  167. 'First',
  168. 'Second',
  169. 'Third',
  170. 'Last'
  171. );
  172.  
  173. foreach($array as $key => $value)
  174. {
  175. if(end($array) === $value)
  176. {
  177. echo "last index!" . $value;
  178. }
  179. }
  180.  
  181. $arr = range(1, 3);
  182.  
  183. $it = new CachingIterator(new ArrayIterator($arr));
  184. foreach($it as $key => $value)
  185. {
  186. if (!$it->hasNext()) echo 'Last:';
  187. echo $value, "n";
  188. }
  189.  
  190. $strReturn = (string) "";
  191.  
  192.  
  193. # Compile the set:
  194. foreach ($aArr as $sItem) {
  195. $strReturn .= $sItem . $sSep;
  196. }
  197.  
  198. # Easy strip the end:
  199. $strReturn = str_replace($sSep . $sAdd, "", $strReturn . $sAdd);
  200.  
  201. return $strReturn;
Add Comment
Please, Sign In to add comment