HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
array / [ ]array_push() / []in_array() / array_search()sort() / rsort()array_map() / array_filter()array_sum() / count()Associative arr (key=>value)
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›REFERENCE›Array
📋

Array

Working with indexed and associative (keyed) arrays.

CommandDescription
array / [ ]Creates a collection of values.
array_push() / []Adds an element to the array.
in_array() / array_search()Does a value exist in the array?
sort() / rsort()Sorts the array (in place).
array_map() / array_filter()Transform every element / filter based on a condition.
array_sum() / count()Total / count of elements.
Associative arr (key=>value)Array accessed with keys (like a dictionary).

array / [ ]

Try it yourself →

Creates a collection of values.

Syntax

$name = [e1, e2, ...];

Description

Arrays are created using square brackets. Elements are accessed with 0-based indexing.

Example

<?php
$colors = ["red", "purple", "green"];
echo $colors[0];
echo "\n";
echo count($colors);

Expected output

red
3

When to use?

Holding lists (names, products).

Common mistakes

  • ⚠️ Indexing starts at 0: the first element is $arr[0].

array_push() / []

Try it yourself →

Adds an element to the array.

Syntax

$arr[] = value;  ·  array_push($arr, value)

Description

$arr[] = x is the shortest way to add; array_push does the same.

Example

<?php
$basket = ["apple"];
$basket[] = "pear";
echo implode(",", $basket);

Expected output

apple,pear

When to use?

Growing lists.

in_array() / array_search()

Try it yourself →

Does a value exist in the array?

Syntax

in_array($value, $arr)

Description

in_array returns true/false, while array_search returns the position (or false if not found).

Example

<?php
$a = ["x", "y", "z"];
echo in_array("y", $a) ? "exists" : "none";

Expected output

exists

When to use?

Membership check (is it selected, is it allowed?).

sort() / rsort()

Try it yourself →

Sorts the array (in place).

Syntax

sort($arr)

Description

sort sorts ascending, rsort sorts descending. Modifies the array in place.

Example

<?php
$s = [3, 1, 2];
sort($s);
echo implode(",", $s);

Expected output

1,2,3

When to use?

Sorting scores/names.

array_map() / array_filter()

Try it yourself →

Transform every element / filter based on a condition.

Syntax

array_map($fn, $arr) · array_filter($arr, $fn)

Description

array_map transforms every element; array_filter selects those matching the condition.

Example

<?php
$kareler = array_map(fn($x) => $x * $x, [1, 2, 3]);
echo implode(",", $kareler);

Expected output

1,4,9

When to use?

List transformation and filtering.

array_sum() / count()

Try it yourself →

Total / count of elements.

Syntax

array_sum($arr) · count($arr)

Description

array_sum sums the numbers; count gives the number of elements.

Example

<?php
echo array_sum([10, 20, 30]);
echo "\n";
echo count([1, 2, 3, 4]);

Expected output

60
4

When to use?

Cart total, average (sum/count).

Associative arr (key=>value)

Try it yourself →

Array accessed with keys (like a dictionary).

Syntax

$name = ["key" => value];

Description

You use your own keys instead of numeric indices (using =>).

Example

<?php
$person = ["name" => "Alex", "age" => 36];
echo $person["name"];

Expected output

Alex

When to use?

Structured data (user, product), JSON.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code