← Back to PHP Course | Chapter 7: Arrays | Lesson 7 of 13

PHP Indexed Arrays

एक indexed array एक numbered list है, किसी queue जैसा जहाँ हर person की zero से शुरू होने वाली एक spot number है। आप उसका number माँगकर एक item पाते हैं।
Syntax
php
$array = [value1, value2, value3];   // keys 0, 1, 2

$array[0];                 // read
$array[0] = new_value;      // overwrite
$array[] = value;          // append

Indexed Arrays बनाना

एक indexed array 0 से शुरू होने वाली automatically assigned, sequential integer keys इस्तेमाल करता है, इसलिए $fruits[0] उस पहले element को refer करता है जो आपने add किया, बिल्कुल ज़्यादातर दूसरी programming languages के arrays जैसे।

उदाहरण: Creating Indexed Arrays

php
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `$fruits[0]` to the output
echo $fruits[0];
?>

Array Elements Access करना

आप किसी element को उसकी numeric position से access या overwrite कर सकते हैं, जैसे $colors[2] = blue;, और PHP खुशी से आपको indices skip करने देगा, ऐसे gaps बनाते हुए जिन्हें count() फिर भी सही से account करता है।

उदाहरण: Accessing Array Elements

php
<?php
// Declare `$colors` as an array: `["red", "green", "yellow"]`
$colors = ["red", "green", "yellow"];
// Set `$colors[2]` to 'blue'
$colors[2] = 'blue';
// Print a human-readable dump of `$colors`
print_r($colors);
?>

Arrays में Elements Add करना

$fruits[] = mango; किसी indexed array के अंत में एक नई value append करता है, automatically इसे अगली available integer key assign करते हुए बिना आपको खुद current highest index track करने की ज़रूरत के।

उदाहरण: Adding Elements to Arrays

php
<?php
// Declare `$fruits` as an array: `["apple", "banana"]`
$fruits = ["apple", "banana"];
// Append 'mango' to the end of `$fruits`
$fruits[] = 'mango';
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>

Array Elements हटाना

foreach ($array as $value) किसी indexed array की values पर order में loop चलाने का idiomatic तरीका है, और आमतौर पर एक counter वाले manual for loop से preferred है क्योंकि यह गलती से array के bounds से आगे नहीं चल सकता।

उदाहरण: Removing Array Elements

php
<?php
// Declare `$fruits` as an array: `["apple", "banana", "mango"]`
$fruits = ["apple", "banana", "mango"];
// Loop over `$fruits`, binding each item to `$fruit`
foreach ($fruits as $fruit) {
    // Print `$fruit . "\n"` to the output
    echo $fruit . "\n";
}
?>

Modify और Reindex करना

Indexed arrays ordered lists जैसे tasks की queue या steps की एक sequence के लिए natural fit हैं, जहाँ किसी item की position (पहला, दूसरा, तीसरा) एक descriptive label के बजाय meaning carry करती है।

उदाहरण: Modifying and Reindexing

php
<?php
// Declare `$tasks` as an array: `["Write report", "Send email", "Attend meeting"]`
$tasks = ["Write report", "Send email", "Attend meeting"];
// Loop over `$tasks`, binding each item to `$index => $task`
foreach ($tasks as $index => $task) {
    // Print `($index + 1) . ". " . $task . "\n"` to the output
    echo ($index + 1) . ". " . $task . "\n";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. 1 से count शुरू करना, ताकि $arr[1] पहला element skip कर दे, जबकि PHP indexes 0 से शुरू होती हैं।
  2. unset($arr[1]) इस्तेमाल करना और बाकी indexes के नीचे shift होने की उम्मीद करना, जबकि keys वैसे ही रहती हैं जैसे थीं।
  3. $arr[5] = x से एक element add करना और इसे अंत में होने की उम्मीद करना, जबकि यह exactly key 5 इस्तेमाल करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.