PHP Indexed Array In Hindi | Best #1 Tutorial In Hindi

PHP Indexed Array एक ऐसा array होता है जिसमें values को numerical index (यानी संख्या) के साथ store किया जाता है। इन Arrays का index 0 से शुरू होता है। Indexed Arrays का उपयोग तब किया जाता है जब data को क्रमबद्ध तरीके से संग्रहीत करना हो, जैसे items की list या numbers की series.

Php Indexed Array के Features:

  1. Data को sequential order (क्रमबद्ध) में store करता है।
  2. Index हमेशा 0 से शुरू होता है।
  3. Elements को index के जरिए access किया जाता है।
  4. हर value के लिए index अपने-आप assign हो जाता है।

Php Indexed Array को Define करने के तरीके:

PHP में Indexed Array को दो तरीकों से define किया जा सकता है:

  1. Traditional Syntax (array() function)
  2. Short Syntax ([])

Example:

<?php
  // Traditional तरीके से
  $fruits = array("Apple", "Banana", "Mango");
  
  // Short सिंटैक्स से
  $fruits = ["Apple", "Banana", "Mango"];
?>
PHP

Indexed Array में Value को Access करना:

Indexed Array के elements को उनके index number से access किया जाता है। Index 0 से शुरू होता है।

<?php
$fruits = ["Apple", "Banana", "Mango"];

echo $fruits[0]; // Output: Apple
echo $fruits[1]; // Output: Banana
echo $fruits[2]; // Output: Mango
?>
PHP

Indexed Array को Loop से Access करना:

PHP में loop का उपयोग करके Indexed Array के सभी elements को आसानी से access किया जा सकता है।

Using for Loop:

<?php
  $fruits = ["Apple", "Banana", "Mango"];
  
  for ($i = 0; $i < count($fruits); $i++) {
      echo $fruits[$i] . "\n";
  }
?>
PHP
image 7

Using foreach Loop:

<?php
  $fruits = ["Apple", "Banana", "Mango"];
  
  foreach ($fruits as $fruit) {
      echo $fruit . "\n";
  }
?>
PHP
Php Indexed Array

Indexed Array में Dynamically Value Add करना:

आप किसी भी Indexed Array में नई value dynamically जोड़ सकते हैं।

<?php
  $fruits = []; // Empty Array
  
  $fruits[] = "Apple";  // Index 0 पर value add की
  $fruits[] = "Banana"; // Index 1 पर value add की
  $fruits[] = "Mango";  // Index 2 पर value add की
  
  print_r($fruits);
?>
PHP
image 5

Indexed Array में Functions का Use:

PHP में array के साथ कई built-in functions होते हैं। Php Indexed Array के साथ commonly use किए जाने वाले कुछ functions हैं:

FunctionDescriptionExample
count()Array में elements की संख्या return करता हैcount($fruits)
sort()Array को ascending order में sort करता हैsort($numbers)
rsort()Array को descending order में sort करता हैrsort($numbers)
array_push()Array में नई value जोड़ता हैarray_push($fruits, “Grapes”)
array_pop()Array के last element को हटाता हैarray_pop($fruits)

Example:

<?php
  $numbers = [5, 3, 8, 1];
  
  // Total elements count
  echo count($numbers); // Output: 4
  
  // Array को sort करना
  sort($numbers);
  print_r($numbers); 
  /* Output: 
  Array ( 
      [0] => 1 
      [1] => 3 
      [2] => 5 
      [3] => 8 
    )*/
?>
PHP

Indexed Array के Errors और उनका Solution:

Undefined Index Error:

जब आप Array के ऐसे index को access करते हैं जो exist नहीं करता, तो PHP “Undefined Index” error देता है।

Solution: isset() Function का उपयोग करके हम इस Error को रिमूव कर सकते हैं।

<?php
  $fruits = ["Apple", "Banana"];
  
  if (isset($fruits[2])) {
      echo $fruits[2];
  } else {
      echo "Index 2 का element exist नहीं करता।";
  }
?>
PHP

Conclusion:

  • Indexed Arrays sequential data को store और manage करने के लिए उपयोग किए जाते हैं।
  • Loop और functions का उपयोग करके हम Arrays के elements को आसानी से access और manipulate कर सकते हैं।
  • Indexed Array data को organize और readable बनाने का एक आसान तरीका है।
error: Content is protected!! You are not allowed to copy.