ホームページ  >  記事  >  バックエンド開発  >  PHPの多次元配列

PHPの多次元配列

WBOY
WBOYオリジナル
2024-08-29 12:44:25266ブラウズ

多次元配列は特別なものではなく、別の配列内の配列にすぎません。配列の各インデックスは、単一の要素ではなく別の配列を保持し、これも別の配列または特定の要素を指すことができます。配列内のこれらのサブ配列は、外側の配列から内側の配列に向かって複数の次元を使用してアクセスされます。ディメンションは基本的に、配列内の特定の位置の値にアクセスまたは格納するために必要なインデックスです。 PHP の多次元配列はリアルタイム アプリケーションで頻繁に使用されますが、値にアクセスしたり値を保存したりする際に複数の括弧があり、ある程度の複雑性があるため、単次元配列と比較して多次元配列を扱うのは非常に困難です。特定のインデックスでは、ループの使用が必要です。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文

以下は、PHP における多次元配列の一般的な構文です。ただし、PHP の多次元配列は 2D、3D、4D などになります。配列の次元が高くなるほど管理が難しくなり、配列名の前に追加される括弧も多くなります。

2D 配列の構文:

array(
array(element1, element2, elements3, ...),
array(element1, element2, elements3, ...),
… so on
)

3D 配列の構文:

array(
array (
array(element1, element2, elements3, ...),
array(element1, element2, elements3, ...),
… so on
),
array (
array(element1, element2, elements3, ...),
array(element1, element2, elements3, ...),
… so on
),
… so on
)

PHP で多次元配列を宣言する方法

PHP では、多次元配列にインデックス付けまたは連想配列を付けることができます。連想配列は、インデックス付き配列と比較してより対話的です。 PHP では、キーワード「array」を使用して PHP で多次元配列を宣言する非常に簡単な方法が可能です。別の配列内で配列を宣言するには、キーワード「array」を追加してから、その配列の要素を追加する必要があります。

1. PHP での 2D 配列の宣言

コード:

<?php
$employee_details = array();
$employee_details[ ] = array("Ram", "Agra", "Sr. Engineer");
$employee_details[ ] = array("Raghav", "Delhi", "Jr. Engineer");
?>

または

<?php
$employee_details = array(
array("Ram", "Agra", "Sr. Engineer"),
array("Raghav", "Delhi", "Jr. Engineer"),
);
?>

上に示した 2 番目の方法は非常に理解しやすいため、一般的に使用されます。

2. PHP での 3D 配列の宣言

コード:

<?php
/* Simplest way to declare a  3D array in Php in an indexed manner */
$item_details  = array(
array(
array ("item1", "abc", 100)),
array ("item2", "bcd", 200)),
array ("item3", "def", 300)),
),
array(
array ("item4", "abc4", 100)),
array ("item5", "bcd5", 200)),
array ("item6", "def6", 300)),
),
);
?>

関連付けにキーと値のペアが使用されていないため、上記の宣言は純粋に 3D 配列の 1 つにインデックス付けされています。

PHP で多次元配列を初期化する方法?

多次元配列の初期化とは、配列の特定の位置またはインデックスに値または要素を割り当てることを意味します。 PHP での多次元配列の初期化は、宣言と同様に非常に簡単です。唯一留意すべきことは、サブ配列の初期化中に中括弧を使用することです。多次元配列の値を初期化する際、メイン配列はインデックス付けまたは連想配列にすることができます。以下の例では、メイン配列は Levis、Lee、Denizen などのキーを持つ連想配列です

1. PHP で 2D 配列を初期化する

コード:

<?php
/* It is a multidimensional 2D array of clothes in which the main array holds another arrays of having 2 elements like cloth type and quantity */
/* It is associative kind of array having the data in the form of key => value pairs. So the data at the inner subarray is represented as associated by the key element.*/
$clothes = array(
"Levis" =>     array(
"Cloth_type" => "jeans",
"Quantity" => 20
),
"Pepe" =>        array(
"Cloth_type" => "jeans",
"Quantity" => 100
),
"Lee" =>      array(
"Cloth_type" => "tshirts",
"Quantity" => 50
),
"Denizen" =>   array(
"Cloth_type" => "tops",
"Quantity" => 80
)
);
?>

2. PHP で 3D 配列を初期化する

3D 配列の初期化は 2D 配列と同じです。2 つの唯一の違いは次元です。 3D 配列の初期化には、2D 配列よりも 1 つ多くのインデックスが必要です。配列の次元数が増加すると、それを初期化するインデックスの数も増加します。以下の例では、メイン配列は、それ自体にサブ配列を持つ単純なインデックス付き配列です。また、以下の例のメイン配列を、ブランド名としてキーを使用して 2D 配列で行ったように連想配列にすることもできます。これにより、アクセスおよび保存する際に顧客が理解しやすくなります。

コード:

<?php
/* In this there is a 3D array of clothes in which each element have an array of cloth type, brand and quantity of that particular brand. Each brand has different quantity and cloth type.*/
$clothes = array(
array(
array(
"Brand" => "Levis",
"Cloth_type" => "jeans",
"Quantity" => 20
),
array(
"Brand" => "Levis",
"Cloth_type" => "Tops",
"Quantity" => 100
)
),
array(
array(
"Brand" => "Lee",
"Cloth_type" => "jeans",
"Quantity" => 50
),
array(
"Brand" => "Lee",
"Cloth_type" => "tops",
"Quantity" => 80
)
),
);
?>

PHP での多次元配列へのアクセス

PHP での多次元配列へのアクセスは非常に簡単で、PHP で一般的に使用されるループである for または for each ループを使用して行われます。インデックス付き配列の場合、配列要素へのアクセスは、C、Java などの他の言語と同様に、行番号と列番号 (arr[row_Num][column_Num]) を使用して通常実行できます。

連想配列の場合、多次元配列の要素へのアクセスは、キーと値のペア (key => Value) を使用して行われます。ただし、要素には単純な for または for each ループを通じてアクセスします。多次元配列の要素へのアクセスを明確に理解するには、以下の例を参照してください。

タイプ

PHP 内で多次元配列が存在できる特定の状態はありません。それは特定の状況やシナリオによって異なります。配列の次元はそれに応じて変化します。通常、プログラマは 2D 配列と 3D 配列を使用します。これは、3D 配列を使用すると、それらの管理が少し難しくなるからです。

As we have understood the declaration, initialization and accessing of multidimensional arrays in PHP, it is time for a quick brief explanation with examples.

1. 2D Array in PHP

2D arrays are basically array inside another array. Consider a scenario that a user have 10 books and each book has a different name, cost, type. In this case, the programmer can create an array of book numbers and each element of the main array holds the array which contains details of the book like name, cost, and type.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
/* Multidimensional 2D array for 4 books and each book having a different array containing book name, cost and type. */
$books = array(
array("Fiction ", "Action and Adventure ", 800),
array("Fiction ", "Anthology ", 1000),
array("Non- Fiction ", "Biography ", 600),
array("Non- Fiction ", "Cook Book ", 900)
);
/* Accessing of a 2D array with the row_number and column_number */
for ($row_num = 0; $row_num < 4; $row_num++) {
echo "<p>Book number is  $row_num</p>";
for ($col_num = 0; $col_num < 3; $col_num++) {
// Accessing a particular element in a 2D array
echo $books[$row_num][$col_num];
}
echo "<br>";
}
?>

Output:

PHPの多次元配列

2. 3D Array in PHP

3D arrays are an extension of 2D arrays. 3D arrays contain one more dimension and provides the chance to add more detailed information. Consider a scenario of employee array, in which employee have name, company and year and each employee has a company profile with the attributes id, skills, and profile. Each employee has personal data also with the details of the city, state, and country. In Order, to Store, the Above Data 3D Array Would Be Required.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$Employee = array(array(array("name", "company", "year"),
array("id","skills","profile"),
array("city","state","country")
),
/* array to store the name, company and year of employee*/
array(array("jiya", "Infosys", 2016),
array("ram", "ola", 2017)
),
/* array to store the id, skills and profile of employees */
array(array("E101", "PHP", "developer"),
array("E103", "mysql", "DBA")
),
/* array to store the city, state and country of employees */
array(array("Bangalore", "Karnataka", "India"),
array("San Francisco", "California", "USA")
)
);
?>
<?php
echo "<ul>";
for ( $outermost = 0; $outermost < 3; $outermost++ )
{
echo "<li>The outermost number $outermost";
echo "<ul>";
for ( $row_num = 0; $row_num < 2; $row_num++ )
{
echo "<li> Now displaying the row number $row_num";
echo "<ul>";
for ( $col_num = 0; $col_num < 3; $col_num++ )
{
// accessing the array elements in a 3D array
echo "<li>".$Employee[$outermost][$row_num][$col_num]."</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
?>
</body>
</html>

Output:

PHPの多次元配列

The above example clearly displays the details of the employee along with their skills in a very user-friendly manner. It allows the detailing of each and every employee in a fancy 3d arrays. We are dealing with 3d arrays, in order to access that, we need to first reach to the main array and then to the index which again holds the subarray and then to the elements of its subarray. In this way, accessing to the elements works in the case of multidimensional arrays starting from the outermost to the innermost array. similarly, in real life, there are sub-arrays or detailed things in which multidimensional arrays are used.

Conclusion

The above explanation clearly shows how the multidimensional arrays are used in php along with their basic syntax and initialization. Multidimensional arrays play an important role when it comes to working on real-life problems as they allow the user to store the data in a detailed form. Moreover, as shown above, php allows storing the multidimensional data either in indexed or associative form according to the requirements which makes it more friendly to access and store the data.

以上がPHPの多次元配列の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHPの連想配列 次の記事:PHPの連想配列