PHP配列とは何ですか?

王林
リリース: 2024-08-29 12:42:37
オリジナル
180 人が閲覧しました

PHP の配列はデータ構造の一種であり、これを使用すると、同じデータ型を持つ複数の要素を 1 つの変数に格納するために別の変数を作成する手間を省くことができます。配列は、インデックスまたはキーによってアクセスできる、同様の要素のリストを作成するのに役立ちます。ここでは配列を使用して、各要素を 1 つの変数に格納し、インデックスまたはキーを介して簡単にアクセスできるようにします。 PHPの関数を使用して配列を生成します。

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

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

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

PHP の配列とは何ですか?

配列は、複数の値を 1 つの名前で保存できるデータ構造に他なりません。簡単に言うと、これは複数の値を含めることができる変数です。配列に割り当てられる値は本質的に同種です。これは、すべての値のデータ型が同じである必要があることを意味します。配列に整数値のみが含まれると想定されている場合、その配列に文字または文字列の値を格納することはできません。

配列は、特定の順序に従って項目または値を格納する項目のコレクションと考えることもできます。配列が作成されたら、それにいくつかの値を割り当てることができます。 C や C++ などの言語では、開発者が最初に配列のサイズを割り当てることになっていますが、PHP では項目を必要なだけ自由に追加でき、それに応じて配列のサイズも増加します。

PHP 配列を理解する

ここで、構文と例を使用して PHP の配列を理解していきます。 PHP の配列は、「array()」として記述される配列関数を使用して実装されます。非常に単純な構文を備えているため、配列の使用が非常に簡単になります。以下は、PHP で配列を宣言して初期化するための構文です。

$array_name = array("value1", "value2", "value3",..)
ログイン後にコピー

こちら

  • Array_name は、値で構成される配列の名前です。
  • Array() は、PHP で配列のメカニズムを実装するのに役立つ関数です。
  • 二重引用符内の値は、配列に割り当てられる値です。

上記の構文を使用して、値を配列に割り当てることができます。さて、配列に格納された値をどのように使用できるかわかりますか?配列値の使用方法の構文を理解してみましょう。

$array_name[index]
ログイン後にコピー

こちら

  • Array_name は、上記の構文で定義した配列の名前です。
  • インデックスは値の位置です。たとえば、「value1」のインデックス値は 0 です。したがって、それらの value1 を使用するには、$array_name[0].
  • のように記述する必要があります。

PHP Array を使用すると、どのようにして作業が簡単になりますか?

配列を使用するとプログラムを操作できるという事実は信じられません。このセクションでは、配列が実際にどのように作業を容易にするかを理解し、PHP 配列の機能を正確に把握するためにいくつかの例を検討します。

例: 下の図は複数の生徒の名前を受け入れ、それを印刷します。

<?php
$student_name=array("Amit", "Raj", "Dhiraj", "Shyam");
echo $student_name[0];
echo "<br>";
echo $student_name[1];
echo "<br>";
echo $student_name[2];
echo "<br>";
echo $student_name[3];
?>
ログイン後にコピー

出力: 上記のプログラムはすべての生徒の名前を出力します。以下は出力です。

アミット
ラージ
ディラージ
シャム

以下の図は、配列の概念を非常に明確に示しています。これは、配列に追加される項目に値が割り当てられる順序を示します。

PHP配列とは何ですか?

PHP 配列の種類

PHP には、1 次元配列と多次元配列という 2 種類の配列があります。

1. One-dimensional array

  • The one-dimensional array may be defined as the type of array that consists of multiple values assigned to the single array.
  • The Values assigned in the one-dimensional array are homogenous in nature that means it allows only the values with the same data type to be added to the array.
  • Syntax will be like.
    $array_name = array("value1", "value2", "value3")
    ログイン後にコピー
  • The way to use the values from the single dimensional array is the same that we have discussed above. $array_name[2] could be used to bring “Value3” in use.

2. Multi-dimensional array

  • A multi-dimensional array may be defined as the kind of array that allows us to assign values in multiple arrays at the same time.
  • The values assigned to the multi-dimensional array could be heterogeneous that means values of different data types could be stored in the arrays and it has to be taken care that the sequence of items of specific data types should be same in all the arrays.
  • Here we will look on two-dimensional array syntax. Well, just for your information, the complexity of array increases as the dimension of the array increases.
$array_name = array
(
array("String1", "Int1", "Int11"),
array("Stirng2", "Int2", "int22"),
array("String3", "Int3", "int33"),
array("String4", "Int4", "int44"),
)
ログイン後にコピー
  • To access the value stored in two-dimensional array we will use below syntax.
$array_name[0][0] will represent "String1"
$array_name[0][1] will represent "int1"
$array_name[0][2] will represent "int11"
$array_name[2][0] will represent "String3"
$array_name[2][1] will represent "int3"
$array_name[2][2] will represent "int33"
ログイン後にコピー

What can you do with PHP Array?

Array in PHP is very helpful while dealing with complex programs. It plays a sheer crucial role in order to make the program less bulky. For illustration, in order to store 10 values we will need 10 different variables but by the use of an array in PHP, we can store 10 different values of the same data type in one array. By the virtue of array, the program could be made very efficient as it helps to save the memory that is usually consumed by the allocation of variables

For simple programs, a one-dimensional array could be very helpful and if the program is somewhat complex than a developer can leverage the multi-dimensional array to get the expected outcome. In PHP, there is no need to assign the data type of the array. It automatically detects the data type by analyzing the values or items assigned to it. In addition to all the features of an array, it’s characteristic of increasing the size automatically makes it favorite among the developers.

Working with PHP array

In the above sections, we learned how an array is implemented in PHP, the way it stores the value and how we can use the values assigned to the array. Now in this section, we see how array could be used together with some of the inbuilt functions to get the desired output. As we know the best way to learn anything is facilitated by examples, the same way we will have a look in an example to dive deeper on working with an array.

Example: In this example, we will use count function with the array to find the total number of students in a school.

<?php
$studnet_name[0]="Amit";
$studnet_name[1]="Raj";
$studnet_name[2]="Dhiraj";
$studnet_name[3]="Shyam";
$arraylenght = count($studnet_name);
echo "The total number of students is" . $arraylenght;
?>
ログイン後にコピー

Output: The count function will count the number of items assigned to the array and that will help us find the total number of students.

The total number of students is 4

The way we added items to the array is different that we used initially. It has been used here this way to give you an idea of the alternate way of assigning values to the array. Name of the four students are assigned to each index of the array and could be used by choosing the index accordingly. let’s see another example where we will be using the sort and count function collectively to sort the values in the array.

Example: In this example, we will assign several roll numbers to the array in some random order and then will make the use of sort function to arrange the order of roll numbers.

<?php
$roll_number= array("22", "13", "2", "9");
sort($roll_number);
$roll_length = count($roll_number);
echo "Roll numbers in ascending order :"
for($x = 0; $x < $roll_length; $x++)
{
echo $roll_number[$x] . " ";
}?>
ログイン後にコピー

Output: The roll numbers that are assigned to the array in the random series will get sorted in ascending order.

Roll numbers in ascending order: 2 9 13 22

Advantages

We have been watching out the pros of using the PHP array and now we will be arranging those in a single section. Below are some of the advantages of PHP array that makes it very useful.

1. Multiple values under one name

An Array allows us to add multiple items of a similar type under one name which makes the values easy to access and convenient as well. Though the values are supposed to be of the same data type, we will not need to define the type of the array.

2. Endorse data structure implementation

The data structures such as linked list, trees, queues are implemented in any high-level programming language by the use of an array. Due to this advantage of an array, the languages could provide a platform to work on complex applications as well. It actually facilitates the way data has to be stored in the memory.

3.行列表現

多次元配列を使用すると、開発者は配列に保持されているのと同じ方法で値を配置することで行列の演算を実行できます。行列を扱う際に注意しなければならない唯一の注意点は、出力は 1 次元配列か 2 次元配列である必要があり、それに応じて出力配列を選択していることです。

4.覚えやすい

特定の値を見つけるために覚えておく必要があるのはインデックス番号だけです。すべての値は同じ名前 (実際には配列名) を使用して呼び出したり使用したりできるため、複数の変数名を記憶する必要はありません。

5.メモリ使用量の削減

変数の数が増えると、すべての変数にメモリを割り当てる必要があるため、プログラムによるメモリ使用率も増加しますが、配列の場合は、複数の値を格納できる単一の配列にメモリを割り当てる必要があり、同じものは不要ですメモリ消費量。

必要なスキル

PHP 配列を操作するには、値がどのように配列に割り当てられるか、値をどのように呼び出すかなど、プログラミングに関するいくつかの基本的なことを知っている必要があります。配列を使用してデータ構造を実装するには、配列の動作概念とともにデータ構造部分について理解しておく必要があります。

配列は変数の拡張版にすぎないため、変数についても理解する必要があります。変数が単一の値を格納する方法と同様に、配列がインデックスを使用して単一の値を格納し、複数のインデックスの平均によって追加された単一の値が集まって配列を形成します。プログラミングの基礎をよく理解していれば誰でも、配列を非常に簡単に操作できます。

なぜ PHP 配列が必要なのでしょうか?

この質問に対する答えを得るには、このチュートリアルでこれまでに理解したすべてのことを思い出す必要があります。何らかの利点が得られる場合にのみ、何かを使用することを好むのは明らかです。配列はさまざまな面で非常に有益であるため、必要な場合には常に配列を使用することを好みます。コードを書いているとき、配列以外の共通エンティティに対して複数の値を保存する必要があるときは、最初に頭に思い浮かぶのが

です。

前に説明したように、プログラムにデータ構造を導入する唯一の方法は配列を使用することです。ツリー、スタック、キュー、リンク リストは、データの保存方法を定義するデータ構造の一部です。これらのデータ構造を実装するには、配列が唯一かつ最良の選択肢です。機能性に加えて、使い方もとても簡単です。また、メモリの使用を軽減する機会を提供してくれるので、これも必要です。配列を使用するプログラム開発者は常に非常に効率的であり、メモリ消費量も少なくなります。

PHP 配列を学ぶのに適した対象者は誰ですか?

PHP を学びたい人は誰でも、PHP 配列を学ぶのに最適です。配列を学ぶために必要なのは、プログラミングの基礎を理解することだけです。つまり、より具体的に言えば、コーディングの基本を知っていれば誰でも配列をスムーズに理解できるようになります。

Learning Array はキャリアの成長にどのように役立ちますか?

最近では大量のデータを扱う必要があるため、必要なときにいつでもデータを取得できるように、データを体系的に保存することが非常に重要になり、配列が正確に登場します。開発者としてのキャリアをサポートするだけでなく、データ構造テクノロジーへのキャリアを伸ばす機会も提供します

結論

どのプログラミング言語でも、配列は非常に重要な役割を果たすため、複雑なプログラムを開発する際にはほとんどの場合、配列を使用する必要があります。これは、豊富なデータを扱う際に最も必要なものとなる、効率的なメモリ使用のメカニズムを提供します。

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

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!