0% found this document useful (0 votes)
11 views

77-PHP-Arrays and Array functions-10-10-2024 (1)

The document contains multiple PHP scripts demonstrating the use of arrays, including creating, manipulating, and sorting them. It covers various array functions such as array_chunk, array_combine, array_diff, and sorting methods like sort, asort, and ksort. Additionally, it showcases accessing multi-dimensional arrays and iterating through arrays using loops and foreach statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

77-PHP-Arrays and Array functions-10-10-2024 (1)

The document contains multiple PHP scripts demonstrating the use of arrays, including creating, manipulating, and sorting them. It covers various array functions such as array_chunk, array_combine, array_diff, and sorting methods like sort, asort, and ksort. Additionally, it showcases accessing multi-dimensional arrays and iterating through arrays using loops and foreach statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

<!

DOCTYPE html>
<html>
<!--PHP arrays-->
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
echo count($cars);
$arrlength=count($cars);

for($x = 0; $x < $arrlength; $x++) {


echo $cars[$x];
echo "<br>";
}

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");


foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}

$cars = array(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";


echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";

$marks = array("mohammad" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ),
"john" => array(10,20,30 ),
"kumar" => array("physics" => 31, "maths" => 22, "chemistry" => 39 ) );
//Accessing multi-dimensional array values
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for john in maths : ";
echo $marks['john'][1] . "<br />"; //numerical array
echo "Marks for kumar in chemistry : " ;
echo $marks['kumar']['chemistry'] ; //associative array

$colors = array("red","green","blue","yellow");
foreach($colors as $value)
{
echo "$value <br>";
}

?>

</body>
</html>
<!DOCTYPE html>
<!-- array function-->
<html>
<body>
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r($a);
//echo $a;
echo "<br>"."array_chunk-->";
print_r(array_chunk($a,2));
echo "<br>";
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");
echo"array_chunk-->";
print_r(array_chunk($a,2,true));

echo "<br>";
$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
echo "array_combine-->";
print_r(array_combine($a1,$a2));

echo "<br>";
$a=array("Cat","Dog","Horse","Dog","Cat");
echo "array_count_values-->";
print_r(array_count_values($a));

echo "<br>";
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
echo "array_diff-->";
print_r(array_diff($a1,$a2));

echo "<br>";
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(0=>"Rat",1=>"Horse",2=>"Dog");
$a3=array(0=>"Horse",1=>"Dog",2=>"Cat");
echo "array_diff_assoc-->";
print_r(array_diff_assoc($a1,$a2,$a3));

echo "<br>";
$a=array_fill(0,3,"Dog");
echo "array_fill-->";
print_r($a);

echo "<br>";
$a=array(0=>"Dog",1=>"Cat",2=>"Horse");
echo "array_flip-->";
print_r(array_flip($a));

echo "<br>";
$a=array("a"=>"Dog","b"=>"Cat");
echo "array_key_exists-->";
if(array_key_exists("a",$a))
echo "Key exists!";
else
echo "Key does not exist!";

echo "<br>";
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","d"=>"Cat");
echo "array_merge-->";
print_r(array_merge($a1,$a2));

echo "<br>";
$a=array(3=>"Horse",4=>"Dog");
echo "array_merge-->";
print_r(array_merge($a));

echo "<br>";
$a1=array("Dog","Cat");
$a2=array("salo","fido");
echo "array_multisort-->";
array_multisort($a1,$a2);
print_r($a1);
echo "<br>";
print_r($a2);

echo "<br>";
$a1=array("Dog","Dog");
$a2=array("Pluto","Fido");
echo "array_multisort ASC-->";
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
echo "<br>";
echo "array_multisort DESC-->";
print_r($a2);

echo "<br>";
$a=array("Dog","Cat","Horse");
echo "array_pop-->";
array_pop($a);
print_r($a);
echo "<br>";
echo "array_shift-->";
array_shift($a);
print_r($a);

echo "<br>";
$a=array("Dog","Cat");
echo "array_push-->";
array_push($a,"Horse","Bird");
print_r($a);
echo "<br>";
echo "array_unshift-->";
array_unshift($a, "fish");
print_r($a);

echo "<br>";
$a=array("a"=>"Dog","b"=>"Cat");
echo "<br>.array_push-->";
array_push($a,"Horse","Bird");
print_r($a);

echo "<br>.array_rand-->";
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_rand($a,1));

echo "<br>.array_reverse-->";
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_reverse($a,false));

$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo "<br>.array_search-->";
echo array_search("Dog",$a);

echo "<br>.array_search strict-->";


$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search("5",$a,true);

echo "<br>.array_slice-->";
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
echo "<br>.array_splice-->";
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,0,1,$a2);
print_r($a1);

echo "<br>.array_sum-->";
$a=array(0=>"5",1=>"15",2=>"25");
echo array_sum($a);

?>

</body>
</html>
<!DOCTYPE html>
<!-- sort arrays-->
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "sort strings-->";
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo " ";
}

$numbers = array(4, 6, 2, 22, 11);


echo "<br>sort numbers-->";
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo " ";
}

echo "<br>rsort-->";
rsort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo " ";
}

echo "<br>asort--><br>";
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}

echo "ksort--><br>";
ksort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
echo "arsort--><br>";
arsort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}

echo "krsort--><br>";
krsort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}

?>

</body>
</html>

You might also like