Il ciclo FOREACH nel PHP
La struttura di controllo FOREACH è così composta:
foreach (array_expression as $ value)
dichiarazione
foreach (tasto array_expression da $ => $ value)
dichiarazione
un esempio può essere:
<?php
$arr = array( 1 , 2 , 3 , 4 );
foreach ( $arr as & $value ) {
$value = $value * 2 ;
}
// $arr is now array(2, 4, 6, 7)
unset( $value ); // break the reference with the last element
?>
altro esempio:
<?php
$arr = array( "one" , "two" , "three" );
reset ( $arr );
foreach ( $arr as $key => $value ) {
echo "Key: $key ; Value: $value <br />\n" ;
}
?>
Esempio di Recordset con ciclo FOREACH
<?php
$db->set_query("select * from documents where document_in_user = 0"); //1
$documents = $db->result_to_array($db->get_result()); //1foreach ($documents as $key => $row) { //2
$file = "uploads/".rawurldecode($row[‘document_name’]);
if ( file_exists ( $file ) == FALSE ) {
unset($documents[$key]); //3
}
}$documents = array_values($documents); // reindex the array (4)
?>