PHPの多次元配列
多次元配列は特別なものではなく、別の配列内の配列にすぎません。配列の各インデックスは、単一の要素ではなく別の配列を保持し、これも別の配列または特定の要素を指すことができます。配列内のこれらのサブ配列は、外側の配列から内側の配列に向かって複数の次元を使用してアクセスされます。ディメンションは基本的に、配列内の特定の位置の値にアクセスまたは格納するために必要なインデックスです。 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:
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:
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 サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

JWTは、JSONに基づくオープン標準であり、主にアイデンティティ認証と情報交換のために、当事者間で情報を安全に送信するために使用されます。 1。JWTは、ヘッダー、ペイロード、署名の3つの部分で構成されています。 2。JWTの実用的な原則には、JWTの生成、JWTの検証、ペイロードの解析という3つのステップが含まれます。 3. PHPでの認証にJWTを使用する場合、JWTを生成および検証でき、ユーザーの役割と許可情報を高度な使用に含めることができます。 4.一般的なエラーには、署名検証障害、トークンの有効期限、およびペイロードが大きくなります。デバッグスキルには、デバッグツールの使用とロギングが含まれます。 5.パフォーマンスの最適化とベストプラクティスには、適切な署名アルゴリズムの使用、有効期間を合理的に設定することが含まれます。

このチュートリアルでは、PHPを使用してXMLドキュメントを効率的に処理する方法を示しています。 XML(拡張可能なマークアップ言語)は、人間の読みやすさとマシン解析の両方に合わせて設計された多用途のテキストベースのマークアップ言語です。一般的にデータストレージに使用されます

静的結合(静的::) PHPで後期静的結合(LSB)を実装し、クラスを定義するのではなく、静的コンテキストで呼び出しクラスを参照できるようにします。 1)解析プロセスは実行時に実行されます。2)継承関係のコールクラスを検索します。3)パフォーマンスオーバーヘッドをもたらす可能性があります。

文字列は、文字、数字、シンボルを含む一連の文字です。このチュートリアルでは、さまざまな方法を使用してPHPの特定の文字列内の母音の数を計算する方法を学びます。英語の母音は、a、e、i、o、u、そしてそれらは大文字または小文字である可能性があります。 母音とは何ですか? 母音は、特定の発音を表すアルファベットのある文字です。大文字と小文字など、英語には5つの母音があります。 a、e、i、o、u 例1 入力:string = "tutorialspoint" 出力:6 説明する 文字列「TutorialSpoint」の母音は、u、o、i、a、o、iです。合計で6元があります

PHPとPythonにはそれぞれ独自の利点があり、プロジェクトの要件に従って選択します。 1.PHPは、特にWebサイトの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンス、機械学習、人工知能に適しており、簡潔な構文を備えており、初心者に適しています。

PHPの魔法の方法は何ですか? PHPの魔法の方法には次のものが含まれます。1。\ _ \ _コンストラクト、オブジェクトの初期化に使用されます。 2。\ _ \ _リソースのクリーンアップに使用される破壊。 3。\ _ \ _呼び出し、存在しないメソッド呼び出しを処理します。 4。\ _ \ _ get、dynamic属性アクセスを実装します。 5。\ _ \ _セット、動的属性設定を実装します。これらの方法は、特定の状況で自動的に呼び出され、コードの柔軟性と効率を向上させます。

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7
