Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

PHP - Array() function

0 votes
14 views
added Apr 25 in PHP by LC Marshal Lieutenant (14,830 points)

indexed array named $trucks

<?php
$trucks=array("Everest","Pajero","Cruiser");
echo "I love " . $trucks[0] . ", " . $trucks[1] . " and " . $trucks[2] . ".";

//output I love Everest, Pajero and Cruiser
?>

2 Responses

0 votes
responded Apr 25 by LC Marshal Lieutenant (14,830 points)

Associative array named $origin

<?php
$origin=array("Peter"=>"Scotland","Ben"=>"Budapest","Joe"=>"Mumbai");
echo "Joe originally from " . $origin['Joe'];

//output Joe originally from Mumbai
?>
0 votes
responded Apr 25 by LC Marshal Lieutenant (14,830 points)

multidimensional array

<?php
// A two-dimensional array
$trucks=array
  (
  array("Everest",77,66),
  array("Pajero",39,42),
  array("Cruiser",80,70)
  );
  
echo $trucks[0][0].": Ordered: ".$trucks[0][1].". Sold: ".$trucks[0][2]."<br>";
echo $trucks[1][0].": Ordered: ".$trucks[1][1].". Sold: ".$trucks[1][2]."<br>";
echo $trucks[2][0].": Ordered: ".$trucks[2][1].". Sold: ".$trucks[2][2]."<br>";

//output
// Everest: Ordered: 77. Sold: 66
// Pajero: Ordered: 39. Sold: 42
// Cruiser: Ordered: 80. Sold: 70
?>
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...