| Examples |
Here, an example showing some information on a collection. First, the Lua code:
dogs = {
Cody = {
color = "Black",
tail = true,
age = 4
},
Buddy = {
color = "Brown",
tail = false,
age = 3
}
}
local function show_dog_info(name, color, hasTail, age)
print("This dog called " .. name
.. " is " .. age .. " years old.")
print(" It's color is " .. color .. ".")
if hasTail then
print(" And it has a tail.")
end
end
print("The dogs are: <br/>")
print(
table.foreach(
dogs,
function(k, v)
print(k)
print("<br/>")
end
)
)
print("<br/>")
for name, value in each(dogs) do
show_dog_info(name, value['color']
, value['tail'], value['age'])
print("<br/>")
end
Finally, how it is converted to PHP:
<?php
require_once("lc_table_manipulation.php");
$dogs = array(
"Cody" => array( "color" => "Black"
, "tail" => true , "age" => 4 ),
"Buddy" => array( "color" => "Brown"
, "tail" => false , "age" => 3 )
);
function show_dog_info($name, $color, $hasTail, $age) {
print("This dog called " . $name . " is "
. $age . " years old.");
print(" It's color is " . $color . ".");
if ($hasTail) {
print(" And it has a tail.");
}
}
print("The dogs are: <br/>");
print(
table_foreach($dogs,
function ($k, $v) {
print($k);
print("<br/>");
}
)
);
print("<br/>");
while (list($name, $value) = each($dogs)) {
show_dog_info($name, $value['color']
, $value['tail'], $value['age']);
print("<br/>");
}
?>