Sử dụng hàm unset[] để loại bỏ các phần tử mảng trong vòng lặp foreach. Hàm unset[] là một hàm có sẵn trong PHP được sử dụng để hủy đặt một biến đã chỉ định. Hành vi của chức năng này phụ thuộc vào những thứ khác nhau. Nếu hàm được gọi từ bên trong bất kỳ hàm nào do người dùng xác định thì nó sẽ bỏ đặt giá trị được liên kết với các biến bên trong nó, để lại giá trị được khởi tạo bên ngoài nó
Xin chào, tôi tự hỏi nếu ai đó có thể giúp tôi với vấn đề này. Tôi có một mảng chứa cả khóa và giá trị. Khóa/giá trị đầu tiên được sử dụng để lưu trữ tiêu đề nên không cần thiết trong phần còn lại của vòng lặp. Làm cách nào tôi có thể bỏ qua khóa/giá trị đầu tiên đó và hiển thị phần còn lại
The following function [similar to one above] will render an array as a series of HTML select options [i.e. ""]. The problem with the one before is that there was no way to handle, so this function solves that issue.
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
{
$returnStatement = '';
if [$selected == ''] {
$returnStatement .= '';
}
if [isset[$optgroup]] {
foreach [$optgroup as $optgroupKey => $optgroupValue] {
$returnStatement .= '';
foreach [$option[$optgroupKey] as $optionKey => $optionValue] {
if [$optionKey == $selected] {
$returnStatement .= '';
} else {
$returnStatement .= '';
}
}
$returnStatement .= '';
}
} else {
foreach [$option as $key => $value] {
if [$key == $selected] {
$returnStatement .= '';
} else {
$returnStatement .= '';
}
}
}
return $returnStatement;
}
So, for example, I needed to render a list of states/provinces for various countries in a select field, and I wanted to use each country name as anlabel. So, with this function, if only a single array is passed to the function [i.e. "arrayToSelect[$stateList]"] then it will simply spit out a bunch of "
________số 8
$countryList = array[
'CA' => 'Canada',
'US' => 'United States'];
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
0
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
1
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
2
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
3
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
4
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
5
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
6
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
7
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
8
{
$returnStatement = '';
function arrayToSelect[$option, $selected = '', $optgroup = NULL]
9
{
$returnStatement = '';
if [$selected == ''] {
0
$returnStatement .= '';
}
if [$selected == ''] {
1
$returnStatement .= '';
}
if [$selected == ''] {
2
$returnStatement .= '';
}
Ví dụ trên sẽ xuất ra
Hàm array_shift[] loại bỏ phần tử đầu tiên khỏi một mảng và trả về giá trị của phần tử đã loại bỏ
Ghi chú. Nếu các khóa là số, tất cả các phần tử sẽ nhận được các khóa mới, bắt đầu từ 0 và tăng thêm 1 [Xem ví dụ bên dưới]