PHP , Javascript (JS) Array Basics Create And Print

 
PHP , Javascript (JS) Array Basics Create And Print 


What is Array?

Array is a special type of variable which stores multiple value in a single variable by its different index number.

Graphical Array value Storage View.


How To Declare or Create in PHP and Javascript?

In PHP – $a=array(1,2,3,4,5);
PHP Create Array by Keyword array and value inside its parenthesis seperated by comma (,).
Here We can Access value of a by using its its like if we use $a[0] we get
value equal to 1. Since Zero index value is 1.

In Javascript – var a=[1,2,3,4,5];
Javascript Creates Array By using Square Bracket and value separated by comma (,)
Here We can Access value of a by using its its like if we use a[0] we get
value equal to 1. Since Zero index value is 1.

Example 1. How to Decalare and get value of array .

IN PHP

<?php
$a = array(1,2,3,4); //Creating array

//printing values of array using simple method 1
echo "Index 0 Value is" . $a[0]."</br>";
echo "Index 1 Value is" . $a[1]."</br>";
echo "Index 2 Value is" . $a[2];

//printing values of array using for loop method 2

$count=count($a); //Here Count Function Counts The Array Of Length

for($i=0;$i<$count;$i++)
{
echo "Index ".$i." Value is" . $a[$i]."</br>";
}

//printing values of array using foreach loop method 2

foreach($a as $key => $value)
{
echo "Index ".$key." Value is" . $value."</br>";
}
?>

IN Javascript

<html>
<body>
</body>
<script>
var a = [1,2,3,4]; //Creating array

//printing values of array using simple method 1
console.log( "Index 0 Value is" +a[0]);
console.log( "Index 1 Value is" +a[1]);
console.log( "Index 2 Value is" +a[2]);

//printing values of array using for loop method 2

var count=a.length; //Here .length Function Counts The Array Of Length

for(var i=0;i<count;i++)
{
console.log( "Index "+i+" Value is" +a[i]);
}

//printing values of array using foreach loop method 2

for(var key in a)
{
console.log("Index "+key+" Value is" +a[key]);
}
</script>
</html>

Thank You
Previous Post
Next Post

post written by:

9 comments: