frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

PHP Arrays

PHP Arrays

Indexed arrays

<!DOCTYPE html>
<html>
<body>


<?php
$cars = array("Volvo", "BMW", "Toyota"); 
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>


</body>
</html>

count() - Return the length of an array

<!DOCTYPE html>
<html>
<body>


<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>


</body>
</html>


Loop through an indexed array

<!DOCTYPE html>
<html>
<body>


<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);


for($x = 0; $x < $arrlength; $x++) {
  echo $cars[$x];
  echo "<br>";
}
?>


</body>
</html>


Associative arrays

<!DOCTYPE html>
<html>
<body>


<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>


</body>
</html>

Loop through an associative array

<!DOCTYPE html>
<html>
<body>


<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");


foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>


</body>
</html>


Tagged:
Sign In or Register to comment.

Developer

| make vanilla clean and light
@ 2016 Your Sitename, All rights reserved. Material Design is a design language developed by Google. Material Design makes more liberal use of grid-based layouts and responsive animations and transitions.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

info@example.com
support@example.com
(800) 3032120

Get In Touch