Learn PHP


array


An array is a normal PHP variable, but we can put other variables inside it. Each variable inside an array is called an element. Each element has a key and a value, which can be any other variable.


Two types of arrays


Indexed arrays - where each element is referenced by a numeric index, usually starting from zero. For example, the first element has an index of 0, the second has an index of 1, and so on


Associative arrays (hash or map) - each element is referenced by a string index.


PHP Create Indexed Array


Indexed arrays is arrays where each element is referenced by a numeric index, usually starting from zero. For example, the first element has an index of 0, the second has an index of 1, and so on


PHP has built-in support for arrays, and we can create array using the array() function.


$myarray = array(element0, element1, element2,element3,...);


		  	<?php
			$myarray = array("PHP", "Mysql", "android","java"); 
			$size = count($myarray); 
			print_r($myarray); 
			?>
		  


Array interpolation


To print array data inside a string, we have to use braces {} around the variable. This next code shows how.


		  	<?php
			$myarray['array'] = "bar";
			print "This is from an array: {$myarray['array']}";
			?>
		  

PHP Create Associative Arrays


PHP Associative Arrays are array with your own keys.


We can use array() function to create associative array.


		  	<?php
			$myarray = array("a"=>"java", "b"=>"android", "c"=>"php"); 
			var_dump($myarray); 
			?>
		  

PHP converts floating-point numbers to integers in the associative arrays, which essentially rounds them down.


Example - Create Associative Array with different type of values


		  	<?php
			$myBook = array( "title" =>  "Learn PHP from studentsempire.com", 
			                 "author" =>  "studentsempire.com", 
			                 "pubYear" =>  2018 );   

			?>
		  




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o