Showing posts with label for-each. Show all posts
Showing posts with label for-each. Show all posts

Sunday, 18 April 2021

Php: for-each loop

Using for-each loop, you can iterate over an array effectively. Foreach loop iterate over the elements of array one by one.

 

Syntax

foreach(array_name as key => value){
    
}

 

Example

foreach ($arr1 as $key => $val) {
    echo "$key => $val\n";
}

 

for_each_demo.php

#!/usr/bin/php

<?php
$arr1 = [
    "id" => 1,
    "age" => 32,
    "name" => "Sailu",
    "male" => false
];

echo "Contents of arr1 are : \n";
foreach ($arr1 as $key => $val) {
    echo "$key => $val\n";
}

?>

 

Output

$./for_each_demo.php 

Contents of arr1 are : 
id => 1
age => 32
name => Sailu
male =>

 

If you are interested in only the values of array, you can use below syntax.

 

Syntax

foreach(array_name as value){
    
}

Example

foreach ($arr1 as $val) {
    echo "$val\n";
}


for_each_iterate_values.php

#!/usr/bin/php

<?php
$arr1 = [
    "id" => 1,
    "age" => 32,
    "name" => "Sailu",
    "male" => false
];

echo "Contents of arr1 are : \n";
foreach ($arr1 as $val) {
    echo "$val\n";
}

?>


Output

$./for_each_iterate_values.php 

Contents of arr1 are : 
1
32
Sailu





 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Sunday, 12 April 2020

TableSaw: Print table contents using for-each loop

Below snippet prints table contents row wise using for-each loop.

for (Row row : table) {
  System.out.println(row);
}

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.Row;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

  public static void main(String args[]) {
    int[] empIds = { 1, 2, 3, 4, 5 };
    String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Hareesh" };
    String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Baji" };

    Table table = Table.create().addColumns(IntColumn.create("Employee Id", empIds))
        .addColumns(StringColumn.create("FirstName", firstNames))
        .addColumns(StringColumn.create("LastName", lastNames));

    for (Row row : table) {
       System.out.println(row);
    }
  }
}

Output
Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           2  |        Ram  |    Gurram  |
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           3  |     Sowmya  |       Maj  |
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           4  |      Chamu  |       Dev  |
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           5  |    Hareesh  |      Baji  |



Previous                                                    Next                                                    Home