PHP Create Arrays
In this page:
$array = [value1, value2, value3];
$array = array(value1, value2, value3); // older syntax
$array = []; // empty array
Short Syntax से एक Array बनाना
एक array बनाने का modern तरीका square brackets है: $fruits = ["Apple", "Banana", "Cherry"]। यह short syntax concise है, array items access करने के लिए इस्तेमाल होने वाले syntax से match करता है, और current PHP code में recommended style है।
उदाहरण: Creating an Array with Short Syntax
<?php
// Declare `$fruits` as an array: `["Apple", "Banana", "Cherry"]`
$fruits = ["Apple", "Banana", "Cherry"];
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
array() से एक Array बनाना
पुराना array() function-call syntax, $fruits = array("Apple", "Banana", "Cherry"), short [] syntax जैसा ही identical array produce करता है -- यह पूरी तरह supported है और पुराने codebases में अभी भी common है, लेकिन short syntax अब आमतौर पर नए code के लिए preferred है।
उदाहरण: Creating an Array with array()
<?php
// Declare `$fruits`, set to `array("Apple", "Banana", "Cherry")`
$fruits = array("Apple", "Banana", "Cherry");
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
Empty शुरू करके Values भरना
एक array को creation time पर किसी values की ज़रूरत नहीं -- $items = [] एक पूरी तरह empty array से शुरू होता है, जिसे आप बाद में भर सकते हैं, एक बार में एक value, अक्सर एक loop के अंदर जो कुछ दूसरे data के आधार पर array के contents बनाता है।
उदाहरण: Starting Empty and Filling In Values
<?php
// Declare `$items` as an empty array
$items = [];
// Append "first" to the end of `$items`
$items[] = "first";
// Append "second" to the end of `$items`
$items[] = "second";
// Print a human-readable dump of `$items`
print_r($items);
?>
Login to try C/C++/Java/PHP code in the editor
एक Specific Size और Default Values के साथ Array बनाना
array_fill(startIndex, count, value) एक दी गई length का array बनाता है जहाँ हर element same default value पर set होकर शुरू होता है, जो असली values भरने से पहले एक grid या default settings के एक set जैसा fixed-size structure pre-allocate करने के लिए उपयोगी है।
उदाहरण: Creating an Array with a Specific Size and Default Values
<?php
// Declare `$grid`, set to `array_fill(0, 5, 0)`
$grid = array_fill(0, 5, 0);
// Print a human-readable dump of `$grid`
print_r($grid);
?>
Login to try C/C++/Java/PHP code in the editor
एक Range से Array बनाना
range(start, end) function start से end (inclusive) तक की हर value वाला एक array generate करता है, default रूप से 1 से step करते हुए -- बिना loop लिखे numbers या यहाँ तक कि letters का एक sequential array बनाने का एक तेज़ तरीका।
उदाहरण: Creating an Array from a Range
<?php
// Declare `$numbers`, set to `range(1, 5)`
$numbers = range(1, 5);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>
Login to try C/C++/Java/PHP code in the editor
- नए code में habit से पुराना array(...) syntax इस्तेमाल करना, जबकि shorter [...] syntax (PHP 5.4 से available) modern, preferred style है।
- यह मान लेना कि किसी array को उसकी सभी values के साथ पहले से बनाया जाना ज़रूरी है, जबकि एक empty array से शुरू करके एक loop में values append करना अक्सर ज़्यादा natural approach है।
- एक ही array के अंदर indexed और associative-style creation को inconsistently mix करना, इसका intended structure एक नज़र में पढ़ना मुश्किल बनाते हुए।
- Short syntax, $arr = [1, 2, 3], PHP में एक array literal बनाने का modern, preferred तरीका है।
- पुराना array(1, 2, 3) function-call syntax अभी भी identically काम करता है, लेकिन इसे legacy style माना जाता है।
- एक array empty भी शुरू हो सकता है, $arr = [], और assignment या [] append syntax इस्तेमाल करके बाद में भरा जा सकता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: