Array — PHP Technical Interview Questions & Answers

Array

Array

  • An array holds many values. Indexed: $marks = [72, 40, 88]; $marks[0] is 72. Associative: $student = ['name' => 'Asha', 'roll' => 12]; $student['name'] is Asha. foreach ($student as $key => $value) walks them.
  • count($arr) length. array_push / $arr[] = 10 to add. PHP arrays are ordered maps — that’s why both styles work.
  • Trap: mixing 0 and '0' carelessly. Trap: foreach ($arr as &$v) then using $v later (reference leak).

Technical Interview Tip: Indexed [72,40] and associative ['name'=>'Asha'].

Code Examples for Array

Array — working example

<?php
$langs = ["PHP", "JS", "SQL"];
$langs[] = "Python";
foreach ($langs as $i => $lang) {
  echo "$i: $lang\n";
}

Array — pHP arrays are ordered maps. foreach walks key and value.

How to Explain Array to an Interviewer

Two array styles?

Indexed 0,1,2 Associative key => value