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

PHP Create Arrays

आप values का एक collection store या इस पर loop चलाने से पहले, आपको पहले उन्हें रखने के लिए actually एक array बनाना होगा। PHP आपको यह करने के कुछ अलग तरीके देता है -- short array literal syntax, पुराना array() function form, और एक empty बनाकर बाद में इसे भरना -- और यह जानना कि हर एक के लिए कब जाना है आपके code को ज़्यादा naturally पढ़ने लायक बनाता है।
Syntax
php
$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
<?php
// Declare `$fruits` as an array: `["Apple", "Banana", "Cherry"]`
$fruits = ["Apple", "Banana", "Cherry"];
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>

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
<?php
// Declare `$fruits`, set to `array("Apple", "Banana", "Cherry")`
$fruits = array("Apple", "Banana", "Cherry");
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>

Empty शुरू करके Values भरना

एक array को creation time पर किसी values की ज़रूरत नहीं -- $items = [] एक पूरी तरह empty array से शुरू होता है, जिसे आप बाद में भर सकते हैं, एक बार में एक value, अक्सर एक loop के अंदर जो कुछ दूसरे data के आधार पर array के contents बनाता है।

उदाहरण: Starting Empty and Filling In Values

php
<?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);
?>

एक 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
<?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);
?>

एक 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
<?php
// Declare `$numbers`, set to `range(1, 5)`
$numbers = range(1, 5);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>
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. नए code में habit से पुराना array(...) syntax इस्तेमाल करना, जबकि shorter [...] syntax (PHP 5.4 से available) modern, preferred style है।
  2. यह मान लेना कि किसी array को उसकी सभी values के साथ पहले से बनाया जाना ज़रूरी है, जबकि एक empty array से शुरू करके एक loop में values append करना अक्सर ज़्यादा natural approach है।
  3. एक ही 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 इस्तेमाल करके बाद में भरा जा सकता है।

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.