PHP 8.5.0 Alpha 1 available for testing

Voting

: max(six, eight)?
(Example: nine)

The Note You're Voting On

j dot vd dot merwe at enovision dot net
14 years ago
A sample how to unset array elements from an array result coming from a mysql request. In this sample it is checking if a file exists and removes the row from the array if it not exists.

<?php
$db
->set_query("select * from documents where document_in_user = 0"); //1
$documents = $db->result_to_array($db->get_result()); //1

foreach ($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)
?>

variables:
mysql table = documents,
array = $documents
array key (index) = $key
array row (record sort of speak) = $row

explanation:

1.
it gets the array from the table (mysql)

2.
foreach goes through the array $documents

3.
unset if record does not exist

4.
the array_values($documents) reindexes the $documents array, for otherwise you might end up in trouble when your process will start expecting an array starting with key ($key) 0 (zero).

<< Back to user notes page

To Top