التخطي إلى المحتوى الرئيسي

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?

تعليقات

  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);
    ?>

    ردحذف
  2. أزال المؤلف هذا التعليق.

    ردحذف
  3. kritika Barod

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

    print_r(array_unique($a));

    ردحذف
  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);

    ردحذف
  5. Kritika Barod

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

    ردحذف
  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);

    ردحذف
  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";

    ردحذف
  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 ";

    ردحذف
  9. Sort Elements In Ascending Order
    Shubham Nandwal
    $ar=array(32,14.5,64,1,78);

    sort($ar);

    print_r($ar);

    ردحذف
  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));

    ردحذف
  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));

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...