Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for(int i=0; i< arr.length;i++){
- boolean isLastElem = i== (arr.length -1) ? true : false;
- }
- $numItems = count($arr);
- $i = 0;
- foreach($arr as $key=>$value) {
- if($i+1 == $numItems) {
- echo "last index!";
- }
- $i++;
- }
- $last_key = end(array_keys($array));
- foreach ($array as $key => $value) {
- if ($key == $last_key) {
- // last element
- } else {
- // not last element
- }
- }
- foreach($input as $key => $value) {
- $ret .= "$value";
- if (next($input)==true) $ret .= ",";
- }
- $toEnd = count($arr);
- foreach($arr as $key=>$value) {
- if (--$toEnd) {
- echo "last index! $value";
- }
- }
- foreach($arr as $key=>$value) {
- //something
- }
- echo "last index! $key => $value";
- $keys = array_keys($array);
- for ($i = 0, $l = count($array); $i < $l; ++$i) {
- $key = $array[$i];
- $value = $array[$key];
- $isLastItem = ($i == ($l - 1));
- // do stuff
- }
- // or this way...
- $i = 0;
- $l = count($array);
- foreach ($array as $key => $value) {
- $isLastItem = ($i == ($l - 1));
- // do stuff
- ++$i;
- }
- for ($i=0;$i<count(arr);$i++){
- $i == count(arr)-1 ? true : false;
- }
- end(arr);
- arr[1];
- $first = true;
- foreach($array as $key => $value) {
- if ($first) {
- $first = false;
- // Do what you want to do before the first element
- echo "List of key, value pairs:n";
- } else {
- // Do what you want to do at the end of every element
- // except the last, assuming the list has more than one element
- echo "n";
- }
- // Do what you want to do for the current element
- echo $key . ' => ' . $value;
- }
- $first = true;
- foreach($array as $key => $subArray) {
- if ($first) {
- $string = "List of key => value array pairs:n";
- $first = false;
- } else {
- echo "n";
- }
- $string .= $key . '=>(';
- $first = true;
- foreach($subArray as $key => $value) {
- if ($first) {
- $first = false;
- } else {
- $string .= ', ';
- }
- $string .= $key . '=>' . $value;
- }
- $string .= ')';
- }
- echo $string;
- List of key => value array pairs:
- key1=>(v1_key1=>v1_val1, v1_key2=>v1_val2)
- key2=>(v2_key1=>v2_val1, v2_key2=>v2_val2, v2_key3=>v2_val3)
- key3=>(v3_key1=>v3_val1)
- $arr = range(1, 10);
- $end = end($arr);
- reset($arr);
- while( list($k, $v) = each($arr) )
- {
- if( $n == $end )
- {
- echo 'last!';
- }
- else
- {
- echo sprintf('%s ', $v);
- }
- }
- $rev_array = array_reverse($array);
- echo array_pop($rev_array);
- <?php
- $week=array('one'=>'monday','two'=>'tuesday','three'=>'wednesday','four'=>'thursday','five'=>'friday','six'=>'saturday','seven'=>'sunday');
- $keys = array_keys($week);
- $string = "INSERT INTO my_table ('";
- $string .= implode("','", $keys);
- $string .= "') VALUES ('";
- $string .= implode("','", $week);
- $string .= "');";
- echo $string;
- ?>
- end( $elements );
- $endKey = key($elements);
- foreach ($elements as $key => $value)
- {
- if ($key == $endKey) // -- this is the last item
- {
- // do something
- }
- // more code
- }
- $numItems = count($arr);
- $i=0;
- $firstitem=$arr[0];
- $i++;
- while($i<$numItems-1){
- $some_item=$arr[$i];
- $i++;
- }
- $last_item=$arr[$i];
- $i++;
- $array = array(
- 'First',
- 'Second',
- 'Third',
- 'Last'
- );
- foreach($array as $key => $value)
- {
- if(end($array) === $value)
- {
- echo "last index!" . $value;
- }
- }
- $arr = range(1, 3);
- $it = new CachingIterator(new ArrayIterator($arr));
- foreach($it as $key => $value)
- {
- if (!$it->hasNext()) echo 'Last:';
- echo $value, "n";
- }
- $strReturn = (string) "";
- # Compile the set:
- foreach ($aArr as $sItem) {
- $strReturn .= $sItem . $sSep;
- }
- # Easy strip the end:
- $strReturn = str_replace($sSep . $sAdd, "", $strReturn . $sAdd);
- return $strReturn;
Add Comment
Please, Sign In to add comment