prisskreative
8/20/2014 - 7:50 PM

PHP Arrays

PHP Arrays

// couples names in a variable

<?php 

 $names = array('name1' , 'name2' , 'name3'); 
 

 ?>
 
//array group and organize our data

define array as color ex:

color = red,green,blue
fruit = orange, peaches, strawberries

//recall arrays by key : value



<?php

$array_example = array();

$color = array('red' , 'green' , 'blue'); 
                 0        1         2

//result - key 0 has a value red

array
(
	[0] => red
	[1] => green
	[2] => blue
)

// echo color 0 = red
echo $color[0];

// print result on screen
print_r($color);

?>

--------------------------------------

//Modify standard array with the key value array

<?php

$color = array('red' , 'green' , 'blue'); 

//change color green to yellow 
$color[1] = 'yellow';


// echo color 1 = yellow after change
echo $color[1];
?>


--------------------------------------

//Add ne value to color array

<?php

$color = array('red' , 'green' , 'blue');


// print result on screen
print_r($color);

//[] this should be blank to add new value
$color[] = 'purple';


// result
array
(
	[0] => red
	[1] => green
	[2] => blue
	[3] => purple
)

--------------------------------------

//Mix data types in a single array

<?php

$hamptom = array(12, 'grey' , 2.5, TRUE); 

// result
array
(
	[0] => 12
	[1] => grey
	[2] => 2.5
	[3] => 1
)


// echo color 1 = yellow after change
echo $hampton[1];
?>