Skip to main content

Array Tutorials in PHP, Array in PHP


It is a collection of elements of the similar and dissimilar datatype, We can store element dynamically without providing size in PHP array.
base index of the array will 0 to size-1, for example, the array has five elements then the array index will be started from 0 to 4.
Advantage of Array:-
1)  We can store multiple elements using a single variable which is easy for program code.
2)  We can easily search and sort the elements  
Type of Array:- 
 1)  Index Array:-  it will store data using an index where the base index will be started from 0 to size-1.
$arrayname = array(12,23,34,11,56,67,78);
$arrayname = array("c",12,23.45,"hello");
$arrayname[0]=12
....

Example of Array:-
<?php
$arr = array(12,23,34,11,67,78);

for($i=0;$i<count($arr);$i++)
{
    echo $arr[$i],"<br>";
}
?>

Example of Array using Foreach Loop:-
<?php
$arr = array("C",1200,"CPP",3450,"DS",56700,79800,"welcome");
for($i=0;$i<count($arr);$i++)
{
echo $arr[$i]."<br>";
}

foreach ($arr as $arr1) {
echo $arr1,"<br>";
}
?>

Program To reverse array element in PHP?
<?php
$arr = array(12,23,34,11,67,78,88,98);
for($i=count($arr)-1;$i>=0;$i--)
{
    echo $arr[$i],"<br>";
}
?>
Program to calculate the sum of array elements in PHP?
<?php
$arr = array(12,23,34,11,67,78,88,98);

$s=0;
for($i=0;$i<count($arr);$i++)
{
    $s=$s+$arr[$i];
}
echo $s;
?>
 2)   Associative Array:-
 Using this we can store elements using key=>value pair where the key is used to provide identity and value is used to provide data.
for example, rno=>1001 here rno is key and 1001 is values.
$stu = array("rno"=>1001,"sname"=>"manish kumar","branch"=>"CS","fees"=>45000);
here rno,sname,branch and fees are the keys (=> association)  
Example of Associative Array:-
<?php
$stu = array("rno"=>1001,"sname"=>"manish kumar","branch"=>"CS","fees"=>45000);
foreach($stu as $key=>$value)
{
    echo "$key  is $value"."<br>";
}  
?>
.....................................................................................
Q1)  WAP to find max, second max element in an array?
<?php
$arr = array(12,20,23,11,21,17);
$max = $arr[0];
$smax= $arr[0];
for($i=1;$i<count($arr);$i++)
{
   if($max<$arr[$i]) //12>20
   {
  $smax=$max;
    $max= $arr[$i];
   }
   else if($smax<$arr[$i] && $smax!=$max)
   {
    $smax=$arr[$i];
   }
}
echo $max,"<br>",$smax;
?>
Q2)  WAP to sort the elements of the array in asc order?
Q3) WAP to calculate the sum of even and the odd number of elements?
3)  Mixed Array:-
  it is used to collect the similar and dissimilar types of elements using index and associative array.
$x = array(10,11,"rno"=>1001,"name"=>"manish kumar");
we can print mixed array using foreach loop.
Example of Mixed array in PHP:-
<?php
$arr = array(12,23,"rno"=>1001,"CPP","JAVA","fees"=>45000,true,12.345);
foreach ($arr as $key => $value) {
echo "key is $key and value is $value"."<br>";
}
?>

The PRedefine function of the array in PHP:-
print_r():-  It will display the structure of array elements
$arr = array(12,23,"hello","hi")
print_r($arr);
max():-   It will display maximum element in the numerical array.
$arr = array(12,23,34,45.67)
max($arr);
min():-   It will display minimum element in the numerical array.
$arr = array(12,23,34,45.67)
min($arr);
array_sum():-  It will display sum of array elements into a numeric array
$arr = array(12,23,34,45.67)
array_sum($arr)
sort():-  it will sort array elements in ascending order
$arr = array(12,23,34,45.67,2,3)
sort($arr)
print_r($arr)
in_array():-  it will check that element is exist in an array or not.it return a boolean value.
$arr = array(12,23,34,45.67,2,3)
if(in_array(12,$x))
{
 echo "found";
}
else
{
   echo "not found";
}
is_array():-  It will check that variable is normal or array type.
$x=10;
if(is_array($x))
{
    echo "array";
}
else
{
  echo "not array";
}
array_merge():-  it is used to merge two arrays in one array.
$a = array(10,11,12);
$b = array(45,67,89,10);
$c = array_merge($a,$b);
print_r($c);
array_intersect():-   It will display common elements in two array.
$a = array(10,11,12);
$b = array(45,67,89,10);
$c = array_intersect($a,$b);
print_r($c);
array_diff():-  It will display the first array element which not exist on second.
$a = array(10,11,12);
$b = array(45,67,89,10);
$c = array_diff($a,$b);
print_r($c);
array_unique():-  It will display unique elements only and repeated elements display once.
$d = array(2,3,4,2,7,9,11,90,3,4,4,4,5);
$d1 = array_uniqe($d);
print_r($d1);
array_splice():-  It is used to split array elements using an index.
$e = array_splice($d, 6);
print_r($e);
array_chunk():-   It is used to divide one dimension array to a multidimensional array. we will provide number of elements on each row.
$d = array(2,3,4,2,7,9,11,90,3,4,4,4,5);
$e1 = array_chunk($d, 6);
print_r($e1);
array_combine():-  It is used to convert index array to associative array but both arrays should be equal size.
$key = array("rno","sname");
$val = array(1001,"xyz");
$data = array_combine($key,$val);
print_r($data);
A complete example of array predefine functions in PHP:-
<?php
$x = array(12,23,34,11,67,78);
print_r($x);
echo max($x)."<br>";
echo min($x)."<br>";
echo array_sum($x)."<br>";
sort($x);
foreach($x as $y)
{
 echo $y."<br>";
}
if(in_array(121,$x))
{
 echo "found";
}
else
{
   echo "not found";
}
$x=array();
if(is_array($x))
{
    echo "array";
}
else
{
  echo "not array";
}
$a = array(10,11,12);
$b = array(45,67,89,10);
$c = array_merge($a,$b);
print_r($c);
$a = array(10,11,12);
$b = array(45,67,89,10);
$c = array_intersect($a,$b);
print_r($c);
$a = array(10,11,12);
$b = array(45,67,89,10);
$c = array_diff($a,$b);
print_r($c);
$d = array(2,3,4,2,7,9,11,90,3,4,4,4,5);
$d1 = array_unique($d);
print_r($d1);
$e = array_splice($d, 6);
print_r($e);
echo "<br>";
$d = array(2,3,4,2,7,9,11,90,3,4,4,4,5);
$e1 = array_chunk($d, 6);
print_r($e1);
$key = array("rno","sname");
$val = array(1001,"xyz");
$data = array_combine($key,$val);
print_r($data);
?>
ASSIGNMENT:-
Create a complete mark sheet program using array?
The solution to this program:-
<?php
$marks=array("phy"=>45,"chem"=>75,"maths"=>89,"eng"=>77,"hin"=>55);
$count=0;
$total=0;
foreach ($marks as $key => $value) {
if($value<33)
{
$count++;
}
else
{
$total=$total+$value;
}
}
if($count==0)
{ $per=$total/5;
if($per>=33 && $per<45)
    echo "Pass with third division";
    else if($per<60)
    echo "Pass with second division";
    else
    echo "Pass with first division";       
}
else if($count==1)
{
    echo "SUPPL";
}
else
{
   echo "Failed";
}
?>
WAP to display only unique elements in an array?
example   2,2,3,4,5,6,7,5   o/p   3 4 6 7
WAP to display repeated elements in array only once?
example  2,2,3,4,5,6,7,5   o/p   2 5
WAP to merge three arrays in one array?
WAP to sort only prime elements in an array?
WAP to find second max prime elements in the array?

Comments

  1. "banana", 2, "monkey", 3);
    $array2 = array("a", "b", "fruit" => "apple", "city" => "paris", 4, "c");
    $array3 = array("a","d","priya" => "paliwal",6,"p");

    // Merging arrays together
    $result = array_merge($array1, $array2, $array3);
    print_r($result);
    ?>

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. kritika Barod

    $a=array(2,2,3,4,5,6,7,5);

    print_r(array_unique($a));

    ReplyDelete
  4. Kritika Barod

    $a=array(1,2,3,4,5);
    $b=array('f','g','h','i','j');
    $c=array(11,12,13,14,15);
    $m = array_merge($a,$b,$c);
    print_r($m);

    ReplyDelete
  5. Kritika Barod

    $input= array(2,2,3,4,5,6,7,5);
    $result=array_unique($input);
    print_r($result);

    ReplyDelete
  6. function occurredOnce(&$arr, $n)
    {

    sort($arr);


    if ($arr[0] != $arr[1])
    echo $arr[0]." ";


    for ($i = 1; $i < $n - 1; $i++)
    if ($arr[$i] != $arr[$i + 1] &&
    $arr[$i] != $arr[$i - 1])
    echo $arr[$i]." ";


    if ($arr[$n - 2] != $arr[$n - 1])
    echo $arr[$n - 1]." ";
    }

    $arr = array(2,2,3,4,5,6,7,5);
    $n = sizeof($arr);
    occurredOnce($arr, $n);

    ReplyDelete
  7. Shubham Nandwal

    SUM OF ONLY EVEN NUMBERS
    $ar=array(120,5,2,4,4,10,5,56,20);
    $sum=0;
    for($i=0;$i<count($ar);$i++)
    {
    if($ar[$i]%2==0)
    {
    $sum=$sum+$ar[$i];
    }

    }
    echo "SUM OF ONLY EVEN NUMBER IS $sum";

    ReplyDelete
  8. SUM OF EVEN AND ODD NUMBERS
    Shubham Nandwal
    $a=array(1,20,42,4,5,7,22);
    $n=0;

    for($i=0;$i<count($a);$i++)
    {

    $n=$n+$a[$i];
    }

    echo " SUM OF EVEN ODD NUMBER IS $n ";

    ReplyDelete
  9. Sort Elements In Ascending Order
    Shubham Nandwal
    $ar=array(32,14.5,64,1,78);

    sort($ar);

    print_r($ar);

    ReplyDelete
  10. No Repetition,Print Only Unique Elements
    Shubham Nandwal
    $ar=array("a"=>10,"b"=>2,"c"=>30,"d"=>10,"e"=>2,"f"=>4,"g"=>"black","i"=>"black");

    print_r(array_unique($ar));

    ReplyDelete
  11. Shubham Nandwal
    Merge Three Arrays In One Array

    $ar1=array(1,2,3,4,5);
    $ar2=array("a","b","c","d");
    $ar3=array("name"=>"tina","roll no"=>8845,"class"=>"11th");

    print_r($ar=array_merge($ar1,$ar2,$ar3));

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...