🟡 Control Flow  ·  Lesson 10

Arrays in PHP

Arrays in PHP

PHP Arrays

An array stores multiple values in one variable. PHP has indexed (numbered) and associative (key-value) arrays.

Indexed Array

<?php
  $marks = [88, 92, 79];
  echo $marks[0];        // 88
  echo count($marks);    // 3
?>

Associative Array

<?php
  $student = ["name" => "Aman", "marks" => 88];
  echo $student["name"];   // Aman
  echo $student["marks"];  // 88
?>

Looping with foreach

<?php
  $prices = ["pen" => 10, "book" => 50];
  foreach ($prices as $item => $price) {
    echo "$item: $price<br>";
  }
?>
pen: 10 book: 50

Summary

  • Indexed arrays use numbers; associative use key=>value.
  • Use foreach to loop; count() for size.

PHP Arrays

Array कई values एक variable में रखता है। PHP में indexed (numbered) और associative (key-value) arrays होते हैं।

Indexed Array

<?php
  $marks = [88, 92, 79];
  echo $marks[0];        // 88
  echo count($marks);    // 3
?>

Associative Array

<?php
  $student = ["name" => "Aman", "marks" => 88];
  echo $student["name"];   // Aman
  echo $student["marks"];  // 88
?>

foreach से Loop

<?php
  $prices = ["pen" => 10, "book" => 50];
  foreach ($prices as $item => $price) {
    echo "$item: $price<br>";
  }
?>
pen: 10 book: 50

सारांश

  • Indexed arrays numbers; associative key=>value use करते हैं।
  • Loop को foreach; size को count()
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।