---
title: Arrays
description: "> ## What is an Array? > > > > An array is a special variable, which can hold more than one value at a time. > > > > Багц нь тусгай хувьсагч бөгөөд нэг удаад нэгээс илүү утга…"
url: "https://apprentice.mn/docs/arrays/"
updated: "2021-07-08"
category: PHP
---

# Arrays

> ## What is an Array?
>
>
>
> An array is a special variable, which can hold more than one value at a time.
>
>
>
> Багц нь тусгай хувьсагч бөгөөд нэг удаад нэгээс илүү утга агуулж чаддаг.
>
>
>
> If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
>
>
>
> Хэрэв танд зүйлсийн жагсаалт байгаа бол (жишээлбэл, автомашины нэрсийн жагсаалт), машиныг ганц хувьсагч дотор хадгалах нь дараах байдалтай байж болох юм.
>
>    $cars1 = “Volvo”;
> $cars2 = “BMW”;
> $cars3 = “Toyota”;
>
> However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
>
>
>
> Гэсэн хэдий ч, хэрэв та машинуудыг тойрон гарч, тодорхой нэгийг нь олохыг хүсвэл яах вэ? Хэрэв танд 3 машин биш, 300 машин байсан бол яах уу?
>
>
>
> The solution is to create an array!
>
>
>
> Үүний шийдэл нь багц үүсгэх явдал юм!
>
>
>
> An array can hold many values under a single name, and you can access the values by referring to an index number.
>
>
>
> Багц нь олон утгыг нэг нэрээр агуулах боломжтой бөгөөд та индексийн дугаараас иш татан утгад хандах боломжтой.

---

## Create an Array in PHP

In PHP, the `array()` function is used to create an array:

PHP дээр `array()` функцийг багц үүсгэхэд ашигладаг:

   array();

In PHP, there are three types of arrays:

PHP дээр гурван төрлийн багц байдаг:

- **Indexed arrays** – Arrays with a numeric index
  Индексжүүлсэн багцууд – Тоон индекс бүхий багцууд
- **Associative arrays** – Arrays with named keys

   Холбоотой багцууд – Нэрлэсэн түлхүүрүүд бүхий багцууд
- **Multidimensional arrays** – Arrays containing one or more arrays
  Олон хэмжээст багц – Нэг буюу хэд хэдэн багцыг агуулсан багцууд

---

   

---

## Get The Length of an Array – The count() Function

The `count()` function is used to return the length (the number of elements) of an array:

`count()`  функцийг багцын уртыг (элементийн тоог) буцаахад ашигладаг.

### Example

### Жишээ

      `<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>`

> ## Complete PHP Array Reference
>
>
>
> For a complete reference of all array functions, go to our complete PHP Array Reference.
>
>
>
> Бүх багцын функцүүдийн талаар бүрэн лавлахыг хүсвэл манай PHP Array лавлагаа руу орно уу.
>
>
>
> The reference contains a brief description, and examples of use, for each function!
>
>
>
> Лавлагаа нь товч тодорхойлолт, ашиглалтын жишээг функц тус бүрт агуулсан болно!

        [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Farrays%2F&linkname=Arrays](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Farrays%2F&linkname=Arrays)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Farrays%2F&linkname=Arrays](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Farrays%2F&linkname=Arrays)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Farrays%2F&linkname=Arrays](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Farrays%2F&linkname=Arrays)[https://www.addtoany.com/share](https://www.addtoany.com/share)
