SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to forward array pointer?

    Hi guys,

    I have a foreach(), I wanted to forward the array pointer but it won't work.
    Below is what I have done so far.
    Code:
     	function next_arr() {
    		$transport = array(
    						'num1' => 'one',
    						'num2' => 'two',
    						'num3' => 'three',
    						'num4' => 'four',
    						'num5' => 'five', 
    						'num6' => 'six',
    						'num7' => 'seven', 
    						'num8' => 'eight',
    						'num9' => 'nine',
    						'num10' => 'ten');
    		
    		foreach($transport as $transpo) {
    			echo $transpo .'<br>';
    			next($transport);			
    		}
    So how do I forward the array pointer inside foreach() using next()?

    Thank you very much in advanced.
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    When you use a foreach it will work its way through each element in an array in turn. Are you trying to do something according to what the data in the element that it's processing?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast Strider64's Avatar
    Join Date
    Aug 2012
    Posts
    43
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $transport 
    = array(
                
    'num1' => 'one',
                
    'num2' => 'two',
                
    'num3' => 'three',
                
    'num4' => 'four',
                
    'num5' => 'five'
                
    'num6' => 'six',
                
    'num7' => 'seven'
                
    'num8' => 'eight',
                
    'num9' => 'nine',
                
    'num10' => 'ten'
                
    );


    function 
    next_arr($transport) {
            
            echo  
    current($transport) . '<br>';
            
    next($transport);
            
    }



    next_arr($transport);
    foreach (
    $transport as $key => $value){            
            
    next_arr($transport);    
    }

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @SpacePhoenix

    I understand how foreach() works.
    I just wanted to control foreach() pointer so it will forward the array pointer.
    Quality codes are optimized and tested...
    Click here for inspiration..

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @Stride65
    Thanks due.
    But it doesn't work.
    Quality codes are optimized and tested...
    Click here for inspiration..

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    So how do I forward the array pointer inside foreach() using next()?
    The short answer is: you don't.

    Calling next() within a foreach will have no effect, as you have seen. This is because foreach() does not use the same "array pointer" that functions like current(), next(), reset(), etc. use. This can clearly be seen with an example like below:

    Code php:
    $letters = range('a', 'h');
    foreach ($letters as $letter) {
        echo current($letters), PHP_EOL;
    }
     
    /* Outputs:
    b
    b
    b
    b
    b
    b
    b
    b
    */

    To get the effect of moving the array pointer forward there are several options (below are only examples, there are many more ways to get the same result).

    1. foreach() with continue
    2. foreach() only what you need
    3. for() loop


    foreach() with continue

    Code php:
    $print = true;
    foreach ($letters as $letter) {
        if ($print = !$print) {
            continue;
        }
        echo $letter, PHP_EOL;
    }

    foreach() only what you need

    There are many ways to do this but the idea is to only loop over the items you want to be looping over.

    Code php:
    // Get every 2nd key
    $keys = array_map('reset', array_chunk(array_keys($letters), 2));
    foreach ($keys as $key) {
        echo $letters[$key], PHP_EOL;
    }

    for() loop

    This is most like what you originally tried to do, but uses for() to iterate over the array manually.

    Code php:
    for (reset($letters); key($letters) !== null; next($letters)) { // basically a foreach() loop
        echo current($letters), PHP_EOL;
        next($letters); // Skip next letter
    }

    Hopefully that will give you some ideas.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @Salathe

    I have formulated my own codes,
    Code:
    	<?php
    		$j=1;
    		if(!empty($records)) {
    			foreach($records as $record) {			
    				//for($j=1; $j<=2; $j++) {
    					if($j>2) {
    						$j=1;						
    					}
    					if($j==1) {
    						echo "<div class='row'>";
    					}
    						if($j<=2) {
    							echo "<div class='span6'>";
    						}
    	?> 
    
    				        	<div id="contee" align="left">
    								<table border="0">
    									<tr>
    										<td align="left">
    											<a href="<?php echo $record["title_url"]; ?>">
    												<?php echo '<h2>'. $record['title'] .'</h2>'; ?>
    											</a>
    										</td>
    									</tr>							
    								</table>				        		
    				        	</div>
    
    	<?php
    						if($j<=2) {
    							echo "</div>";
    							//forward one step array pointer
    							//next($records);
    						}
    					if($j==2) {
    						echo "</div>";
    					}
    
    					$j++;
    				//}
    			}
    		}
    	?>
    @salathe
    Your answers is very nice dude.
    Thanks anyway.
    Quality codes are optimized and tested...
    Click here for inspiration..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •