Cách nào sau đây là cách chấp nhận được để tạo một mảng PHP

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]
{
    $returnStatement = '';
0

function arrayToSelect[$option, $selected = '', $optgroup = NULL]
{
    $returnStatement = '';
1

function arrayToSelect[$option, $selected = '', $optgroup = NULL]
{
    $returnStatement = '';
2

function arrayToSelect[$option, $selected = '', $optgroup = NULL]
{
    $returnStatement = '';
3

function arrayToSelect[$option, $selected = '', $optgroup = NULL]
{
    $returnStatement = '';
4

function arrayToSelect[$option, $selected = '', $optgroup = NULL]
{
    $returnStatement = '';
5

Nếu bạn có một danh sách các mục [ví dụ: danh sách tên ô tô], việc lưu trữ ô tô trong các biến đơn lẻ có thể trông như thế này

$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

Tuy nhiên, nếu bạn muốn đi vòng qua các ô tô và tìm một ô tô cụ thể thì sao?

Giải pháp là tạo một mảng

Một mảng có thể chứa nhiều giá trị dưới một tên duy nhất và bạn có thể truy cập các giá trị bằng cách tham chiếu đến một số chỉ mục

Chủ Đề