How to print a row from 2d array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deizi23
    New Member
    • Nov 2015
    • 6

    How to print a row from 2d array?

    i want to print 1,3,5 rows from 2d array. Can i use some kind of implode or loop?

    $array = array(
    array(11,12,13, 14,15,16),
    array(21,22,23, 24,25,26),
    array(31,32,33, 34,35,36),
    array(41,42,43, 44,45,46),
    array(51,52,53, 54,55,56),
    array(61,62,63, 64,65,66)
    );

    I want it to look like this: "array row1: 11,12,13,14,15, 16"
    Last edited by deizi23; Nov 18 '15, 11:16 AM. Reason: forgot to add some line
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    Comments in the code explain the process
    Code:
    foreach($array as $i=>$array_values){
    	
    	// check for the rows you want and subtract 1 since
    	// array counts start at 0	
      	if($i == 0 || $i == 2 || $i == 4){
      		
      		// turn array into a string split with commas
        	$array_values = implode(",",$array_values);
    		// add + 1 to array index $i to display the right row number
    		$row_num = $i + 1;
    		// output ddesired results
    		echo "row". $row_num ." ".$array_values."<br/>"; 
      
    	}  
    }
    niheel @ bytes

    Comment

    Working...