Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Convert:
dot  Life calculator 
dot  Astronomy 
dot  Length 
dot  Area 
dot  Time 
dot  Weight 
dot  Temperature 
dot  Capacity 
dot  Math 
dot  Density 
dot  Pressure 
dot  Random 
dot  IT converters 
dot  Electricity 
Search

 
 
 
 
Previous article Next article Go to back
       

PHP. Arrays, foreach loop

1. Array
<?php

$a = array(1, 2, 3, 4, 5, 6);

# loop without key variable, only variable for value of element

foreach ( $a as $b ){

    echo "$b";

}

# printing of element
echo $a[0];

?>

1a. Arrays
https://www.w3schools.com/php/php_arrays.asp

https://www.w3schools.com/php/php_arrays_indexed.asp

https://www.w3schools.com/php/php_arrays_associative.asp

2. Keys and values:
<?php

$a = array(1, 10, 11, 10 => 10, 11=>"text", "name" => "john");

foreach ( $a as $r => $b ){

    echo "$r = $b   <br />";

}

// Atskirų elementų spausdinimas:
echo $a[0]. " " . $a["name"];

?>

3. How to change values. $r and $b are not references:
<?php
$a = array(1, 10 , 11 , "john" => 12,  10 => 10 , 11=>"text");


foreach ( $a as $r=>$b ){
    
    // changing values of elements of the old array
    $a[$r] = $b . "labas";
    
    // creation of new array
    $aa[$r] = $b . "labas";

}

$a[22]=22;
$a[33]=345;


// print a new array
foreach ( $aa as $r=>$b ){

    echo "aa $r = $b   <br />";

}

// print an old array
foreach ( $a as $r=>$b ){

    echo "a $r = $b   <br />";

}

?>

4. Array and condition:
<?php

$a = array(-1, 10, 11, 3, 2);

foreach ( $a as $r=>$b ){

    if ($b < 5){ $a[$r] = "red"; } else { $a[$r] = "green"; }
    
}

foreach ( $a as $b ){

    echo "$b<br />";
}

?>

5. Duplicating and changing elements:
<?php

// increase amount of elements

$a = array(1,10,11, 10 => 10,11=>"text");

foreach ( $a as $r=>$b ){
    
    // change value of existing element    
    $a[$r]    = $b . " labas";
    // creating of new element
    $a[$r+100]= $b . " kitas";
    
}

foreach ( $a as $r=>$b ){

    echo "$r = $b   <br />";
    
}

?>

6. Unordered elements are usual:
<?php
// print first 3 places, key variable is $i.
// $i is used to calculate a real position in array.

$arr = array (

    15 =>"ona",
    1  =>"jonas",
    11 =>"petras",
    10 =>"simas",
    12 =>"janina",
    
);

$i = 0;

foreach ($arr as $k => $v){

    $i++;
    
    if ($i <= 3 ){
        echo $v ,"<br />";
    }

}

// we cant print fors 3 element using this way. There is no such indexes
echo "<br />", $arr[0], $arr[1], $arr[2];

?>

7. Recursive print:
<?php

$arr = array (

    1  => "jonas",
    10 => "simas",
    11 => "petras",
    12 => "janina",
    
);

echo "<pre>"; print_r ($arr); echo "</pre>";

?>

8. Wrong situation:
<?php

# data type is not checked

$a = 10;

foreach ( $a as $r=>$b ){

    echo "$r and $b   <br />";

}


?>

8a. Right solution:
<?php

// checking data type

$a = 10;

// uncomment after checking with integer value
// $a  = array (1,2,3);

if ( is_array ($a) ){

    foreach ( $a as $r=>$b ){

        echo "$r and $b  <br />";

    }

}else{

    echo $a;

}

?>


Task 1. We have users and salaries. The number of elements can differ. It means that the new elements can appear and can be deleted:
$arr = array (

    "ona"        => 2000,
    "jonas"      => "not set",
    "bob"        => 1000,
    "petras"     => 1000,
    "john"       => 1500,
    "janina"     => 2000,
    "kate"       => "unknown",
    "john"       => 2000,
    "jim"        => 2000,
    "rocky"      => 1000, 
    "bird"       => 1500,    
    "kim"        => 2000,

);

Please print the last three users, who earn less than 1600EUR. The task must be done without count, prev, next, reset, end functions, using plain arithmetics, loops, and conditions.


Task 2. There is an array. The array can change values and amount of elements:
<?php

$a = array (10, 20, "test1"=>30, "jim", 1 ,50, 1, 1, "john, "peter", "test" => 10);

?>

Please increase numeric values ten times and print them using loop foreach. Only first five integers are affected. Text values are untouched, not changed.

The final output:
array (100, 200, "test1" => 300, "jim", 10 ,500, 1, 1, "john, "peter", "test" => 10)

-
 
Previous article Next article Go to back