PHP Arrays in Hindi
RGPV University / DIPLOMA_CSE / Web Technology
PHP Arrays in Hindi
PHP Arrays in Hindi
PHP Arrays एक महत्वपूर्ण concept है जो प्रोग्रामिंग में डेटा को store करने और manipulate करने के लिए इस्तेमाल होता है। Array एक variable होता है, जो कई values को एक साथ store करता है। इस लेख में हम PHP Arrays के बारे में विस्तार से सीखेंगे।
Types of Arrays in PHP in Hindi
PHP में मुख्य रूप से तीन प्रकार के Arrays होते हैं:
- Indexed Arrays: इस तरह के array में index numbers के द्वारा values को store किया जाता है। इसे numeric index array भी कहा जाता है।
- Associative Arrays: इस array में values को specific keys द्वारा access किया जाता है, ना कि index numbers द्वारा।
- Multidimensional Arrays: यह arrays के array होते हैं, जिसमें हम एक array के अंदर दूसरे array को store कर सकते हैं।
Creating Arrays in PHP in Hindi
PHP में Arrays को create करने के लिए दो main methods होते हैं:
- Using array() function: हम PHP के array() function का उपयोग करके arrays बना सकते हैं।
- Using Short Array Syntax: PHP 5.4 से पहले से ही short array syntax भी उपलब्ध है।
$fruits = array("Apple", "Banana", "Mango");
$fruits = ["Apple", "Banana", "Mango"];
Accessing Array Elements in PHP in Hindi
Array elements को access करने के लिए हम array के index या key का उपयोग करते हैं:
- Indexed Arrays: आप array के index number का उपयोग करके element access कर सकते हैं।
- Associative Arrays: associative array में values को उनके keys के द्वारा access किया जाता है।
echo $fruits[0]; // Output: Apple
$person = array("name" => "John", "age" => 30);
echo $person["name"]; // Output: John
Modifying Array Elements in PHP in Hindi
Array में किसी भी element को modify करने के लिए हम उसी index या key का उपयोग करते हैं, जिसका उपयोग हम access करने में करते हैं:
- Indexed Arrays: किसी भी array element को modify करने के लिए index का उपयोग करें।
- Associative Arrays: associative array में key के माध्यम से element को modify किया जा सकता है।
$fruits[1] = "Grapes"; // Array becomes: ["Apple", "Grapes", "Mango"]
$person["age"] = 31; // Array becomes: ["name" => "John", "age" => 31]
Array Functions in PHP in Hindi
PHP में Arrays के साथ काम करने के लिए कई built-in functions होते हैं। ये functions arrays को manipulate करने में मदद करते हैं।
- count(): इस function का उपयोग array के elements की संख्या पता करने के लिए किया जाता है।
- sort(): इस function का उपयोग array को ascending order में sort करने के लिए किया जाता है।
- array_push(): इस function का उपयोग array के end में एक या ज्यादा elements को add करने के लिए किया जाता है।
- array_pop(): इस function का उपयोग array से आखिरी element को remove करने के लिए किया जाता है।
- array_merge(): इस function का उपयोग दो या दो से ज्यादा arrays को merge करने के लिए किया जाता है।
$count = count($fruits); // Output: 3
sort($fruits); // Sorts the array
array_push($fruits, "Orange"); // Adds "Orange" to the end of the array
array_pop($fruits); // Removes the last element
$combined = array_merge($fruits, $person); // Merges both arrays
FAQs
Array in PHP is a variable that can hold multiple values. Instead of declaring individual variables for each value, you can store them in an array and access them using indices or keys.
There are three types of arrays in PHP:
- Indexed Arrays
- Associative Arrays
- Multidimensional Arrays
You can create an array in PHP using the array()
function or the short array syntax. Example: $fruits = array("Apple", "Banana");
or $fruits = ["Apple", "Banana"];
Array elements can be accessed by using their index (for indexed arrays) or key (for associative arrays). For example:
$fruits[0];
for indexed arrays.$person["name"];
for associative arrays.
You can modify array elements by assigning a new value to the array index or key. For example:
$fruits[1] = "Grapes";
$person["age"] = 35;
Some useful array functions in PHP include:
count()
- To count the number of elements in an array.sort()
- To sort the array in ascending order.array_push()
- To add an element to the end of the array.array_merge()
- To merge two or more arrays.