Sitemap
Anish Regmi

Anish Regmi

Software Developer || Content Creator

PHP Arrays — with Examples

1 min readSep 24, 2025

Array Basics

$arr = ['apple', 'banana']; // ordered map

Array Types

$indexed = ['a','b']; // Indexed
$assoc = ['name'=>'John','age'=>30]; // Associative
$multi = [['name'=>'A'],['name'=>'B']]; // Multi

Array Creation

$a = [1,2,3];
$b = array(1,2,3);
$c = range(1,5); // [1,2,3,4,5]
$d = array_fill(0,3,'x'); // ['x','x','x']
$e = array_combine(['a','b'],[1,2]); // ['a'=>1,'b'=>2]
$f = compact('a','b'); // ['a'=>[1,2,3],'b'=>[1,2,3]]

Access & Modification

$fruits=['apple','banana'];
echo $fruits[0]; // apple
$fruits[]='cherry'; // add
$fruits[0]='apricot'; // modify
unset($fruits[1]); // remove
echo $fruits[3]??'not found'; // safe access

Iteration

foreach($fruits as $i=>$f){ echo "$i:$f "; }
for($i=0;$i<count($fruits);$i++){ echo $fruits[$i]; }
$nums=[1,2,3]; $squared=array_map(fn($n)=>$n*$n,$nums); // [1,4,9]

Sorting

$fruits=['cherry','apple','banana'];
sort($fruits); // ['apple','banana','cherry']
rsort($fruits); // reverse
$ages=['John'=>25,'Alice'=>30,'Bob'=>20];
asort($ages); // sort by value
ksort($ages); // sort by key
usort($fruits,fn($a,$b)=>$a<=>$b); // custom

Searching

in_array('apple',$fruits); // true
array_search('banana',$fruits); // index
array_key_exists('John',$ages); // true
array_keys($ages); // ['John','Alice','Bob']
array_values($ages); // [25,30,20]

Get Anish Regmi’s stories in your inbox

Join Medium for free to get updates from this writer.

Filtering & Transformation

$nums=[1,2,3,4];
$even=array_filter($nums,fn($n)=>$n%2==0); // [2,4]
$squared=array_map(fn($n)=>$n*$n,$nums); // [1,4,9,16]
$sum=array_reduce($nums,fn($c,$i)=>$c+$i,0); // 10
$names=array_column([['id'=>1,'name'=>'A'],['id'=>2,'name'=>'B']], 'name'); // ['A','B']
$unique=array_unique([1,1,2,3]); // [1,2,3]

Combination & Slicing

array_merge([1,2],[3,4]); // [1,2,3,4]
array_replace(['a','b'],[1=>'x']); // ['a','x']
array_slice([1,2,3,4],1,2); // [2,3]
$nums=[1,2,3,4]; array_splice($nums,1,2,['x']); // [1,'x',4]
array_chunk([1,2,3,4],2); // [[1,2],[3,4]]

Stack & Queue

$stack=[]; array_push($stack,'a'); array_pop($stack);
$queue=[]; array_push($queue,'a','b'); array_shift($queue); array_unshift($queue,'x');

Math Ops

array_sum([1,2,3]); // 6
array_product([2,3]); // 6
array_diff([1,2,3],[2,3]); // [1]
array_intersect([1,2,3],[2,3]); // [2,3]

Utility

count([1,2,3]); // 3
array_flip(['a'=>1,'b'=>2]); // [1=>'a',2=>'b']
array_reverse([1,2,3]); // [3,2,1]
array_rand([1,2,3]); // random key
shuffle($nums); // random order
array_pad([1,2],4,0); // [1,2,0,0]
array_fill_keys(['a','b'],'x'); // ['a'=>'x','b'=>'x']
array_count_values(['a','b','a']); // ['a'=>2,'b'=>1]

Serialization

serialize(['a'=>1]); // 'a:1...'
json_encode(['a'=>1]); // '{"a":1}'
implode(',', ['a','b']); // 'a,b'
explode(',', 'a,b'); // ['a','b']

Comparison

[1,2]==[1,2]; // true
[1,2]===[1,'2']; // false

Destructuring

[$a,$b]=[1,2]; // a=1,b=2
[$a,$b]=[$b,$a]; // swap
['name'=>$n]=['name'=>'John']; // n='John'

--

--

Anish Regmi
Anish Regmi

Written by Anish Regmi

Software Developer || Content Creator