If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
PHP में indexed array , store की गयी values को index number के रूप में associate करता है जो कि 0 start होता है।
File : array.php
<?php
$arr1 = array('Student', 'Job', 'Study');
echo "<pre>";
print_r($arr1);
?>
Array ( [0] => Student [1] => Job [2] => Study )
ऊपर दिया गया Example में simple indexed array define किया गया है।
PHP में हम इसके अलावा एक और तरीके से indexed array define कर सकते हैं , जिसमें हमें values को एक साथ नहीं रखते , need के according single value को array में store करते हैं।
See Example :
File : array2.php
<?php
$arr[] = "value1";
$arr[] = "value2";
$arr[] = "value3";
$arr[] = "value4";
echo "<pre>";
print_r($arr);
?>
Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4 )
इस तरह से भी हम PHP में indexed array define कर सकते हैं , जहां index number automatically previous index के accordingly update होकर value को store करता रहता है।
अब अगर हमें array से सिर्फ single value को get करना है तो simply index number के साथ access कर सकते हैं। ऊपर दिए गए example में से अब अगर हमें first और last values चाहिए हो तो कुछ इस तरह से access करेंगे।
For Example :
File : array2.php
<?php
$arr[] = "value1";
$arr[] = "value2";
$arr[] = "value3";
$arr[] = "value4";
echo "first value : ". $arr[0]." , last value :". $arr[3];
?>
PHP में हम आसानी से array values को change / modify कर सकते हैं।
Example :
File : array3.php
<?php
$arr = array('John', 'Tom');
echo 'Before change : <pre>';
print_r($arr);
$arr[0] = 'Hugo';
$arr[1] = 'Diego';
echo '</pre> After Change : <pre>';
print_r($arr);
?>
Array ( [0] => John [1] => Tom )After Change :
Array ( [0] => Hugo [1] => Diego )
array को foreach loop के through आसानी से traverse किया जा सकता है।
File : array4.php
<?php
$arr[0] = 'Hugo';
$arr[1] = 'Diego';
foreach ($arr as $key=> $value)
{
echo $key." = ".$value."<br>";
}
?>
इस तरह से हम array को foreach loop के through traverse करते हैं , हालाँकि $key variables optional होता है , अगर हम के नहीं चाहते तो इसे remove करके direct values को use कर सकते हैं।
Example :
File : array5.php
<?php
$arr[0] = 'Hugo';
$arr[1] = 'Diego';
foreach ($arr as $value)
{
echo $value."<br>";
}
?>
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook