Created
October 10, 2013 04:55
-
-
Save abdullahbutt/6913240 to your computer and use it in GitHub Desktop.
arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$n=10; | |
for($x=0,$y=1,$z,$i=0;$i<$n;$i++) | |
{ | |
if($i==0) | |
{ | |
echo $x."<br>".$y."<br>"; | |
} | |
$z=$x+$y; | |
$x=$y; | |
$y=$z; | |
echo $z."<br>"; | |
} | |
?> | |
<?php | |
$abc=array(1,2,3,'a','b','c',array('Abdullah','Shahryar','Hameed','Butt')); | |
var_dump($abc); | |
$array_reverse_true=array_reverse($abc,true); | |
var_dump($array_reverse_true); | |
$array_reverse_false=array_reverse($abc,false); | |
var_dump($array_reverse_false); | |
?> | |
<?php | |
echo count($abc)."<br>"; | |
for($i=count($abc)-1;$i>=0;$i--) | |
{ | |
$me[]=$abc[$i]; | |
} | |
var_dump($me); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$names=array('alpha','bravo','charlie','delta','echo'); | |
var_dump($names); | |
//rsort($names); //rsort stands for reverse sort | |
for($i=0;$i<count($names);$i++) | |
{ | |
echo $i.' name is '.$names[$i].'<br>'; | |
} | |
echo '---------------------------------------------------------------------------------------------------------------'.'<br>'; | |
$jets=array(array('F-16','F-15','F-14','Su-27'),array('F-22','F-35','J-20','J-31','Su-Pak T-50'),$names,'I Louuu Jets'); | |
//var_dump($jets); | |
echo $jets[0][0]; | |
echo '<br>'; | |
echo $jets[1][0]; | |
echo '<br>'; | |
echo $jets[2][0]; | |
echo '<br>'; | |
echo $jets[3][0]; | |
echo '<br>'; | |
echo $jets[3][2]; | |
echo '<br>'; | |
echo $jets[3]; | |
echo '<br>'; | |
foreach($jets as $i=>$c){ | |
var_dump($c[$i]); // very interesting result .... each array's one index is resulted .... first array's first, 2nd array's 2nd & so on | |
} | |
echo '--------------------------------------"Multidimensional Array"----------------------------------------'.'<br>'; | |
$books=array('fiction'=>array('not_fiction'=>array('scifi'=>array('aliens','robot')) | |
), | |
array('horror'=>array('demons','zombies') | |
) | |
); | |
echo $books['fiction']['not_fiction']['scifi'][1]; //extracted from multi-dimensional array | |
echo '<br>'; | |
$greeting=array('French'=>'Salute', | |
'English'=>'Hello', | |
'Urdu'=>'Salaam'); | |
echo $greeting['French'].'<br>'.'<br>'; | |
foreach($greeting as $key=>$val){ | |
echo $key.' is '.$val.'<br>'; | |
} | |
echo '<br>'; | |
$list=implode(' and ', $names); //implode converts array into string | |
echo $list; | |
echo '<br>'; | |
$sentence="This is a script by Jack"; | |
$words=explode(' ', $sentence); | |
var_dump($words); | |
foreach ($words as $key => $value) { | |
echo $key.' has value --> '.$value.'<br>'; | |
} | |
echo '<br>'; | |
echo '<br>'; | |
for($i=0;$i<count($words);$i++) | |
{ | |
echo $key. ' has value--> '.$words[$i].'<br>'; | |
} | |
echo '<br>'; | |
strlen($sentence); //gives count of length of string | |
echo '<br>'; | |
$abc='This cake can be cut into pieces'; | |
echo substr($abc, 5); //substring .... 2nd parameter starts count from 5th character in string | |
echo '<br>'; | |
echo substr($abc, 5,5);//substring 3rd parameter means string will end at which character starting from 2nd parameter | |
echo '<br>'; | |
$cake='This cake needs to be split in 2 equal halves'; | |
echo substr($cake, 0,strlen($cake)/2).'<br>'; //we get first half of string | |
echo substr($cake, strlen($cake)/2).'<br>'; //we get last half of string | |
$old='Today I got up, put up my old pants, used my old car & went to my old office'; | |
echo str_replace('old', 'new', $old); //str_replace replaces mentioned text within a string with a new one. Here it replaces 'old' with 'new' | |
echo '<br>'; | |
/* | |
$sentence='This sentence contains several words'; | |
if(ereg('word', $sentence)) //NOTE: ereg is case sensitive | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
if(ereg('^this', $sentence)) // ^ means that it (word) is in beginning of the string//NOTE: ereg is case sensitive | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
if(eregi('^this', $sentence)) //NOTE: eregi is case in-sensitive | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
if(ereg('word$', $sentence)) //NOTE: $ sign means to search at end of string. In this case it will return false as 's' is found after word | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
if(ereg('words$', $sentence)) //In this case it will return TRUE as 'words' is found at the end of the string | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
if(ereg('^words$', $sentence)) //In this case it will return FALSE & can return ONLY true IF string ONLY contains 'words' in it as ^ means found in beginning & $ means found at end | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
$sentence='words'; | |
if(ereg('^words$', $sentence)) //In this case it will return FALSE & can return ONLY true IF string ONLY contains 'words' in it as ^ means found in beginning & $ means found at end | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
$sentence='The evil baron laughed: "muwahahahahaha"'; | |
if(ereg('muwa(ha){3}', $sentence)) | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
$sentence='The evil baron laughed: "muwaha"'; | |
if(ereg('muwa(ha)+', $sentence)) //+ means just one instance of ha or more | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
$sentence='The evil baron laughed: "muwa"'; | |
if(ereg('muwa(ha)*', $sentence)) //+ means just one OR ZERO instance of ha | |
{ | |
echo 'Regular Expression found'; | |
}else{ | |
echo 'Regular Expression NOT found'; | |
} | |
echo '<br>'; | |
*/ | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Arrays</title> | |
</head> | |
<body> | |
<h1>Arrays</h1> | |
<?php | |
$month=array('january','February','March','April','May'); | |
$tuts_sites=array('php+','sql+','apache','code_igniter+'); | |
//$tuts_sites=['php+','sql+','apache','code_igniter+'];//Array Can also be written as this in php 5.4 | |
print_r ($month); echo "<br>"; | |
var_dump($month); echo "<br>"; | |
var_dump($month[3]); echo "<br>"; | |
//echo $month; echo "<br>";//only ganna print Array & with notice that Array being converted to String | |
echo $month[3]; echo "<br>"; | |
?> | |
<h2>Tuts Sites</h2> | |
<ul> | |
<?php | |
foreach ($tuts_sites as $site){ | |
echo $site; // concatenates all array data | |
} | |
?> | |
</ul> | |
<ul> | |
<?php | |
foreach($tuts_sites as $site){ | |
echo "<li>$site</li>"; | |
} | |
?> | |
</ul> | |
<ol> | |
<?php | |
foreach($tuts_sites as $site){ | |
echo "<li>$site</li>"; | |
} | |
?> | |
</ol> | |
<select style="animation-delay:" id="1" name="Select"> | |
<option>Select</option> | |
<?php | |
foreach($tuts_sites as $site){ | |
echo "<option>$site</option>"; | |
} | |
?> | |
</select> | |
<?php | |
$my_sites=array( | |
'hotmail'=>'www.outlook.com', | |
'yahoo_mail'=>'www.mail.yahoo.com', | |
'gmail'=>'www.gmail.com', | |
); | |
?> | |
<ul> | |
<?php | |
foreach($my_sites as $sites){ | |
echo "<li>$sites</li>"; | |
} | |
?> | |
</ul> | |
<ul> | |
<?php | |
foreach($my_sites as $name=>$url){ | |
echo "<li><a href=\"$url\">$name</a></li>"; | |
} | |
?> | |
</ul> | |
<ul> | |
<?php | |
foreach($my_sites as $name=>$url){ | |
echo "<li><a href=$url>$name</a></li>"; | |
} | |
?> | |
</ul> | |
<ul> | |
<?php | |
foreach($my_sites as $name=>$url){ | |
echo "<li><a href='$url'>$name</a></li>"; | |
} | |
?> | |
</ul> | |
<ul> | |
<?php | |
foreach($my_sites as $name=>$url){ | |
echo "<li><a href='$url'>".ucwords($name)."</a></li>"; //ucwords is used to capitalize first alphabet | |
} | |
?> | |
</ul> | |
<ul> | |
<?php | |
foreach($my_sites as $name=>$url): ?> | |
<li> | |
<a href="<?=$url;?>"><?=$name;?> </a> | |
</li> | |
<?php endforeach ?> | |
</ul> | |
<?php | |
$edu=array('apsac'=>'www.APSAC.com', | |
'FCC'=>'www.FCC.com', | |
'UMT'=>'www.UMT.com', | |
'TP'=>'www.TP.com'); | |
print_r ($edu); | |
echo "<br>"; | |
?> | |
<ol> | |
<?php foreach ($edu as $my_edu=>$url){ | |
echo "<li><a href='$url'>".ucwords($my_edu)."</a></li>"; | |
} | |
?> | |
</ol> | |
<ol> | |
<?php foreach ($edu as $my_edu=>$url): ?> | |
<li> | |
<a href=<?=$url; ?>> <?=ucwords($my_edu);?></a> | |
</li> | |
<?php endforeach ?> | |
</ol> | |
<?php | |
print_r($month);echo "<br>"; | |
array_push($month,'June'); //add data in push | |
print_r($month); | |
echo "<br>"; | |
var_dump($month); | |
echo "<br>"; | |
$month[]='July'; //add data in push | |
print_r($month); | |
echo "<br>"; | |
array_pop($month); //deletes only the last value in array | |
print_r($month); | |
echo "<br>"; | |
$me=array_pop($month);// over here, we have removed last value for array $month() & stored the value in $me | |
echo $me; | |
echo "<br>"; | |
print_r($month); | |
echo "<br>"; | |
array_shift($month); //remove first value at 0 index of array $month() | |
print_r($month); | |
echo "<br>"; | |
array_unshift($month,'JanuaRy');//Add first value at 0 index of array $month() | |
print_r($month); | |
echo "<br>"; | |
?> | |
<?php | |
$input=array('a','b','c','d','e'); | |
print_r($input); | |
echo "<br>"; | |
print_r($input[2]); | |
echo "<br>"; | |
print_r(array_slice($input,2));//array_slice slices the array. In this case, slices & starts array from index 2 of array $input whic has value c | |
echo "<br>"; | |
print_r($input); | |
echo "<br>"; | |
$output=array_slice($input,2,3);//$output is new array which has array values of $input from index 2 till 3 | |
print_r($output); | |
echo "<br>"; | |
print_r($input);//$input remains unchanged though array_slice values were stored in $output | |
echo "<br>"; | |
$output=array_slice($month,2);//$output is new array which has array values of $input from index 2 till end | |
print_r($output); | |
echo "<br>"; | |
$output=array_slice($month,2,4);//$output is new array which has array values of $input from index 2 till 4 | |
print_r($output); | |
echo "<br>"; | |
$output=array_slice($month,2,-1);//$output is new array which has array values of $input from index 2 till one less the last value of array $month | |
print_r($output); | |
echo "<br>"; | |
$output=array_slice($month,2,-3);//$output is new array which has array values of $input from index 2 till 3 less the last value of array $month which will hence be blank in this case | |
print_r($output); | |
echo "<br>"; | |
$output=array_slice($month,2,-2);//$output is new array which has array values of $input from index 2 till 2 | |
print_r($output); | |
echo "<br>"; | |
$output=array_slice($input,2); //returns c,d,e | |
$output=array_slice($input,-2,1); //returns d | |
$output=array_slice($input,0,3);//returns a,b,c | |
// note the difference in array keys | |
print_r(array_slice($input,2,-1)); //returns Array([0]=>c,[1]=>d) | |
echo "<br>"; | |
print_r(array_slice($input,2,-1,true)); //returns Array([2]=>c,[d]=>d) it changes the index to start from 2 due to "true" | |
echo "<br>"; | |
?> | |
<?php | |
$new_month=array('jan','feb','mar','april','may'); | |
print_r($new_month); | |
echo "<br>"; | |
$new=array_filter($new_month,function($test){return strlen($test)==3; }); | |
print_r($new); //april is missed out as it has 4 charaters & not 3 | |
//recommended to study array_walk & array_map | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Test Aray</title> | |
</head> | |
<body> | |
<?php | |
$sites=array('hotmail','yahoo','gmail','mail'); | |
?> | |
<select style="animation-delay:" name="select" id="mails" > | |
<option>Select</option> | |
<?php | |
foreach ($sites as $site){ | |
echo "<option>$site<option>"; | |
} | |
?> | |
</select> | |
<?php echo "<br>";?> | |
<?php | |
$my_site=array( | |
'yahoo'=>'www.yahoo.com', | |
'hotmail'=>'www.hotmail.com', | |
'gmail'=>'www.gmail.com' | |
); | |
foreach ($my_site as $site=>$url){ | |
echo "<li><a href=$url>$site </a></li>"; | |
} | |
?> | |
<?php echo "<br>";?> | |
<?php | |
foreach ($my_site as $site=>$url){ | |
echo "<li><a href=$url>".ucwords($site)."</a></li>"; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment