> > PHP:

PHP:


9 2012, 11:00. : Design FactoRy
, . , .

: , , .

 PHP:



$arrBooks , :

<?php
$arrBooks = array(
Comic => array(
Title=>Superman,
Author=>Jerry Siegel and Joe Shuster,
Publication Date => 1938),

Science Fiction => array(
Title=>Dune,
Author=>Frank Herbert,
Publication Date=>1965),

Fantasy => array(
Title=>The Hobbit,
Author=>J.R.R. Tolkien,
Publication Date=>1937),

Horror => array(
Title=>Carrie,
Author=>Stephen King,
Publication Date=>1974)
);
?>



, , . , PHP-, :

<?
echo $arrBooks[Science Fiction][Title];
echo "<br>";
echo $arrBooks[Horror][Author];
?>

:

Dune
Stephen King



, foreach. , .

<?
foreach ($arrBooks as $obj_key =>$book)
{
echo "$obj_key Book:<br>";
foreach ($book as $key=>$value){
echo "$key: $value<br>";
}
echo "<br>";
}
?>

:

Comic Book:
Title: Superman
Author: Jerry Siegel and Joe Shuster
Publication Date: 1938

Science Fiction Book:
Title: Dune
Author: Frank Herbert
Publication Date: 1965

Fantasy Book:
Title: The Hobbit
Author: J.R.R. Tolkien
Publication Date: 1937

Horror Book:
Title: Carrie
Author: Stephen King
Publication Date: 1974



. , . is_array , , , sort , . .

count($array): .

<?
$numBooks = count($arrBooks);
echo "There are $numBooks books in the collection.<br>";
?>

There are 4 books in the collection.

extract($array): . .

<?
$arrBooks = array( Comic => Superman,
ScienceFiction => Dune,
Fantasy => The Hobbit,
Horror => Carrie);

extract($arrBooks);
// $arrBooks[Comic] becomes $Comic
// $arrBooks[ScienceFiction] becomes $ScienceFiction
// $arrBooks[Fantasy] becomes $Fantasy
// $arrBooks[Horror] becomes $Horror

echo "$Comic is a comic book.<br>";
echo "$Fantasy is a fantasy book.<br>";

?>

Superman is a comic book.
The Hobbit is a fantasy book.

extract($array, EXTR_PREFIX_ALL, prefix): , .

<?
$arrBooks = array(
Comic => Superman,
ScienceFiction => Dune,
Fantasy => The Hobbit,
Horror => Carrie);

extract($arrBooks, EXTR_PREFIX_ALL, "books");
// $arrBooks[Comic] becomes $books_Comic
// $arrBooks[ScienceFiction] becomes $books_ScienceFiction
// $arrBooks[Fantasy] becomes $books_Fantasy
// $arrBooks[Horror] becomes $books_Horror

echo "$books_Comic is a comic book.<br>";
echo "$books_Fantasy is a fantasy book.<br>";

$arrFilms = array(
Comic => Superman Returns,
ScienceFiction => Terminator,
Fantasy => Dark Crystal,
Horror => Friday the 13th);

extract($arrFilms, EXTR_PREFIX_ALL, "films");
// $arrFilms [Comic] becomes $films_Comic
// $arrFilms [ScienceFiction] becomes $films_ScienceFiction
// $arrFilms [Fantasy] becomes $films_Fantasy
// $arrFilms [Horror] becomes $films _Horror

echo "$films_Comic is a comic film.<br>";
echo "$films_Fantasy is a fantasy film.<br>";
?>

Superman is a comic book.
The Hobbit is a fantasy book.
Superman Returns is a comic film.
Dark Crystal is a fantasy film.

compact(var1, var2, var3): .

<?
$Comic = Batman;
$ScienceFiction = Dreaming Void;
$Fantasy = American Gods;
$Horror = Frankenstein;

$arrBooks2 = compact (Comic, ScienceFiction, Fantasy, Horror);

foreach ($arrBooks2 as $key => $value) {
print "$value is an example of a $key book.<br>n";
}
?>

Batman is an example of a Comic book.
Dreaming Void is an example of a ScienceFiction book.
American Gods is an example of a Fantasy book.
Frankenstein is an example of a Horror book.

, : push, pop, shift, unshift merge.