PHP Indexed Arrays
In this page:
$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
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `$fruits[0]` to the output
echo $fruits[0];
?>
Login to try C/C++/Java/PHP code in the editor
Array Elements Access करना
आप किसी element को उसकी numeric position से access या overwrite कर सकते हैं, जैसे $colors[2] = blue;, और PHP खुशी से आपको indices skip करने देगा, ऐसे gaps बनाते हुए जिन्हें count() फिर भी सही से account करता है।
उदाहरण: Accessing Array Elements
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Arrays में Elements Add करना
$fruits[] = mango; किसी indexed array के अंत में एक नई value append करता है, automatically इसे अगली available integer key assign करते हुए बिना आपको खुद current highest index track करने की ज़रूरत के।
उदाहरण: Adding Elements to Arrays
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Array Elements हटाना
foreach ($array as $value) किसी indexed array की values पर order में loop चलाने का idiomatic तरीका है, और आमतौर पर एक counter वाले manual for loop से preferred है क्योंकि यह गलती से array के bounds से आगे नहीं चल सकता।
उदाहरण: Removing Array Elements
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
Modify और Reindex करना
Indexed arrays ordered lists जैसे tasks की queue या steps की एक sequence के लिए natural fit हैं, जहाँ किसी item की position (पहला, दूसरा, तीसरा) एक descriptive label के बजाय meaning carry करती है।
उदाहरण: Modifying and Reindexing
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- 1 से count शुरू करना, ताकि
$arr[1]पहला element skip कर दे, जबकि PHP indexes 0 से शुरू होती हैं। unset($arr[1])इस्तेमाल करना और बाकी indexes के नीचे shift होने की उम्मीद करना, जबकि keys वैसे ही रहती हैं जैसे थीं।$arr[5] = xसे एक element add करना और इसे अंत में होने की उम्मीद करना, जबकि यह exactly key 5 इस्तेमाल करता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: