Array traversal with loops (foreach) In Hindi
पीएचपी प्रोग्रामिंग में किसी ऐरे एलिमेंट डाटा को स्टार्ट एलिमेंट से एन्ड एलिमेंट तक ट्रैवर्स या लूपिंग करने का मतलब है, की ऐरे एलिमेंट के वैल्यूज को एक्सेस करने या ऐरे डाटा एलिमेंट में मॉडिफिकेशन करने के लिए ऐरे एलिमेंट में foreach लूप के माध्यम से लूपिंग या ट्रैवर्स हर ऐरे एलिमेंट को प्रोसेस और मैनेज करना होता है। पीएचपी प्रोग्रामिंग में किसी ऐरे डाटा एलिमेंट को ट्रैवर्स करने का सबसे कॉमन मेथड में foreach लूप का यूज़ करना है। यहाँ foreach लूप ऐरे ट्रेवेर्सल प्रोसेस को कम्पलीट करने में हेल्प करता है, और इसे इंडेक्स्ड और एसोसिएटिव ऐरे के साथ यूज़ कर सकते है।

basic foreach loop in PHP.
पीएचपी प्रोग्रामिंग में foreach लूप का यूज़ कर किसी ऐरे के प्रत्येक एलिमेंट डाटा पर ट्रैवर्स किया जा सकता है, और प्रोग्रामर को ऐरे के वैल्यू और यह अल्टरनेटिव आर्डर में ऐरे एलिमेंट कीस पर डायरेक्ट एक्सेस प्रोवाइड करता है। पीएचपी प्रोग्रामिंग में foreach लूप को अप्लाई करने के दो कॉमन मेथड हैं.
Two common methods for applying a foreach loop.
- Values only – यह मेथड मौजूदा ऐरे एलिमेंट के वैल्यूज पर रेपीटीशन करता है।
- Keys and values – यह मेथड मौजूदा एक एसोसिएटिव ऐरे में ऐरे कीस और एलिमेंट वैल्यूज दोनों पर रेपीटीशन करता है।
Example of traversing an indexed array (values only) in PHP.
<?php
$countries = [“India”, “Usa”, “Chiha”, “Uk” , “Australia”];
foreach ($countries as $country) {
echo $country . “\n”;
}
?>
In the indexed array (values only) example.
- इस प्रोग्राम में प्रोग्रामर $countries नाम से एक इंडेक्स्ड ऐरे के प्रत्येक एलिमेंट के माध्यम से लूप करने के लिए foreach लूप को अप्लाई करते हैं।
- इसमें वेरिएबल $country प्रत्येक ऐरे एलिमेंट रेपीटीशन के दौरान ऐरे में प्रत्येक ऐरे वैल्यू को रिप्रेजेंट करता है।
Example of traversing an indexed array (key and value) in PHP.
<?php
$countries = [“India”, “Usa”, “Chiha”, “Uk”, “Australia”];
foreach ($countries as $key => $country) {
echo “Index $key: $country \n”;
}
?>
Indexed array (key and value) condition.
- यहाँ इस प्रोग्राम में $key इंडेक्स एलिमेंट (जैसे, 0, 1, 2, 3, 4 आदि) रेफ़्रेन्स को इंडीकेट करता है।
- यहाँ $country वेरिएबल प्रत्येक ऐरे इंडेक्स पर वैल्यू को रिप्रेजेंट करता है।
Traveling an associative array in PHP.
पीएचपी प्रोग्रामिंग में एक असोसिएटिव ऐरे प्रत्येक ऐरे एलिमेंट डाटा की एक स्टोरेज कीस और एक होल्ड वैल्यू स्टोरेज लोकेशन होता है। जिसमे foreach लूप प्रोग्रामर को ऐरे डाटा रेपीटीशन के दौरान और ऐरे वैल्यूज दोनों को आसानी से एक्सेस और प्रोसेस करने की परमिशन प्रोवाइड करता है।
Example of traversing an associative array (key and value).
<?php
$student = [
“stu_name” => “Harry”,
“stu_age” => 27,
“stu_cont” => 9414,
“country” => “Florida”
];
foreach ($student as $key => $stu_info) {
echo “$key – $stu_info \n”;
}
?>
In the example of traversing an associative array.
- इस प्रोग्राम में लूप $student एसोसिएटिव ऐरे में प्रत्येक की-वैल्यू पेअर पर रेपीटीशन करता है।
- यहाँ $student $key कुंजियों (जैसे “stu_name”, “stu_age”, “stu_cont”, “country” ) को डिस्प्ले किया गया है।
- और $student एसोसिएटिव ऐरे $stu_info वैल्यूज (जैसे “Harry”, 27, “9414”, “Florida”) ऐरे एलिमेंट डाटा को डिस्प्ले करता है।
Nested foreach loop for multidimensional array in PHP.
पीएचपी प्रोग्रामिंग में प्रोग्रामर एक मल्टी-डायमेंशनल ऐरे (ऐरे के अंदर ऐरे) को डील कर रहा है, तो पीएचपी प्रोग्रामर मल्टी-डायमेंशनल इंटरनल ऐरे एलिमेंट डाटा को एक्सेस और प्रोसेस करने के लिए नेस्टेड foreach लूप को अप्लाई कर सकते हैं।
Example of traversing a multidimensional indexed array.
<?php
$table = [
[9, 4, 1 ,3],
[1, 9, 7, 5],
[2, 6, 5, 4]
];
foreach ($table as $rows) {
foreach ($rows as $values) {
echo $values . ” “;
}
echo “\n”;
}
?>
In the multidimensional indexed array condition.
- एक्सटर्नल foreach लूप रौस जिनमें से प्रत्येक एक मल्टी-डायमेंशनल ऐरे है, डाटा एलिमेंट पर रेपीटीशन करता है।
- इसमें मल्टी-डायमेंशनल ऐरे में इंटरनल ऐरे में foreach लूप प्रत्येक रौ के वैल्यूज एलिमेंट पर रेपीटीशन करता है।
Example of traversing a multidimensional associative array in PHP.
<?php
$student = [
“Harry” => [“contact” => “111-222-333”, “id” => “101”, “email_id” => “harry@domain.com”],
“Sid” => [“contact” => “444-555-666”, “id” => “102”, “email_id” => “sid@domain.com”]
];
foreach ($student as $stu_name => $stu_info) {
echo “$stu_name’s Student Detail \n”;
foreach ($stu_info as $key => $values) {
echo “$key = $values \n”;
}
echo “\n”;
}
?>
In the multidimensional associative array example.
- इस प्रोग्राम में एक्सटर्नल foreach लूप प्रत्येक स्टूडेंट ऐरे एलिमेंट (“Harry” और “Sid”) डाटा पर रेपीटीशन करता है।
- इसी तरह इंटरनल foreach लूप स्टूडेंट ऐरे डिटेल (जैसे, “contact”, “id”, “email_id”) के अंदर प्रत्येक ऐरे की-वैल्यू पेअर पर रेपीटीशन करता है।
Modifying array elements using a foreach loop in PHP.
पीएचपी प्रोग्रामिंग में प्रोग्रामर foreach लूप के अंदर डायरेक्ट ऐरे एलिमेंट डाटा को भी जरूरत पड़ने पर मॉडिफाई कर सकते हैं। याद रहे, यदि प्रोग्रामर ओरिजिनल ऐरे को मॉडिफाई करना चाहते हैं, तो प्रोग्रामर को उन ऐरे का रेफरेन्सेस को यूज़ करना होगा।
Example of modifying array values using references in PHP.
<?php
$integers = [7, 9, 8, 10, 12];
foreach ($integers as &$integer) {
$integer *= 3; // here it Multiply each integers element with 3 number
}
print_r($integers);
// Result – ( [0] => 21 [1] => 27 [2] => 24 [3] => 30 [4] => 36 )
?>
Modifying array values using references in the condition.
- यहाँ इस प्रोग्राम में प्रोग्रामर ऐरे एलिमेंट डाटा को रेफ़्रेन्स करने के लिए &$integer को यूज़ करते हैं, जिससे जरूरत पड़ने पर प्रोग्राम में हम ऐरे में ओरिजिनल वैल्यू को मॉडिफाई कर सके।
- Loop Important – पीएचपी प्रोग्रामिंग में foreach के साथ & ऑपरेटर का यूज़ करते समय केयरफुल रहें, क्योंकि यह आपके ओरिजिनल ऐरे डाटा टाइप वैल्यू को डायरेक्ट मॉडिफाई करता है। यहाँ foreach लूप यूज़ के बाद, ऐबनार्मल सिस्टम बिहैवियर से बचने के लिए अनसेट मेथड को जरूर यूज़ करे.
unset($number);
Skipping or Breaking a loop in PHP.
पीएचपी प्रोग्रामिंग में प्रोग्रामर नेक्स्ट रेपीटीशन पर जाने के लिए continue कीवर्ड को यूज़ कर सकते हैं, या मौजूदा प्रोग्राम लूप से कम्प्लीटली एग्जिट करने के लिए break कीवर्ड को यूज़ कर सकते हैं।
Example of using continue in foreach in PHP.
<?php
$phones = [“apple iphone”, “motorola”, “samsung”, “nokia”, “oppo” ];
foreach ($phones as $phone) {
if ($phone == “samsung”) {
continue; // here it skip when it reach at “samsung” element
}
echo $phone . “\n”;
}
?>
Using continue in foreach In the example,
इस प्रोग्राम में continue स्टेटमेंट “samsung” ऐरे एलिमेंट पर पहुंचते ही रेपीटीशन छोड़ देता है, और $phones ऐरे में नेक्स्ट आइटम पर मूव करता और ऐरे एलिमेंट को डिस्प्ले करता है।
Example of using break in foreach in PHP.
<?php
$phones = [“apple iphone”, “motorola”, “samsung”, “nokia”, “oppo” ];
foreach ($phones as $phone) {
if ($phone == “nokia”) {
break; // here it stop the loop when it reach at “nokia” element
}
echo $phone . “\n”;
}
?>
In the example using break in foreach.
- यहाँ इस प्रोग्राम में $phones ऐरे में “nokia” एलिमेंट डाटा मिलने पर foreach लूप break स्टेटमेंट लूप को एग्जिट कर देता है, और बचे हुए स्टेटमेंट प्रिंट नहीं होते है।
A summary of the use of the foreach loop in array traversal in PHP.
- Indexed arrays – इंडेक्स्ड ऐरे वैल्यूज पर रेपीटीशन करें या ऐरे कीस और वैल्यूज दोनों को एक्सेस कर मैनेज कर सकते है।
- Associative arrays – असोसिएटिव ऐरे में ऐरे एलिमेंट डाटा में की-वैल्यू पेअर पर रेपीटीशन कर सकते है।
- Multidimensional arrays – मल्टी-डायमेंशनल ऐरे के अंदर ऐरे एलिमेंट पर रेपीटीशन करने के लिए नेस्टेड foreach लूप को यूज़ कर सकते है।
- Modifying an array – पीएचपी प्रोग्रामिंग में ओरिजिनल ऐरे को मॉडिफाई करने के लिए रेफ़्रेन्स एंड (&) ऑपरेटर को यूज़ करें।
- Quitting/Breaking – पीएचपी प्रोग्रामिंग में ऐरे डाटा एलिमेंट में रेपीटीशन को इग्नोर करने के लिए continue कीवर्ड को यूज़ करें और foreach लूप को स्टॉप के लिए break कीवर्ड को यूज़ करें।