PHP/Data Structure/in array — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 10:02, 26 мая 2010

Checking for an element with a particular value

   <source lang="html4strict">

<? $meals = array("A" => 1,

              "B" => 4.95,
              "C" => 3.00,
              "D" => 6.50,
              "E" => 0); 

$books = array("F","G");

if (in_array(3, $meals)) {

 print "There is a $3 item.";

} if (in_array("F", $books)) {

 print "We have F";

} // in_array() is case-sensitive if (in_array("f", $books)) {

 print "We have the f.";

} ?>

 </source>
   
  


Determining Whether an Element Is in an Array

   <source lang="html4strict">

<?php $countries = array("Germany" => "German", "France" => "French", "Spain" => "Spanish"); $language = "Spanish";

printf("

%s of our visitors speak %s.

\n", in_array($language, $countries) ? "Some" : "None", $language);

$language = "asdfasd";

printf("

%s of our visitors speak %s.

\n", in_array($language, $countries) ? "Some" : "None", $language);

?>

 </source>
   
  


in_array() function is a control statement.

   <source lang="html4strict">

<? $language = "French"; $languages = array ("English", "Gaelic", "Spanish"); if (in_array($language, $languages)) :

    print "$language edition.";

else :

    print "We don"t yet offer a $language edition";

endif; ?>

 </source>
   
  


in_array( ) function return true if an array contains a specific value

   <source lang="html4strict">

//bool in_array ( mixed needle, array haystack [, bool strict] ) <?

   $needle = "Sam";
   $haystack = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Joe");
   if (in_array($needle, $haystack)) {
           print "$needle is in the array!\n";
   } else {
           print "$needle is not in the array\n";
   }

?>

 </source>
   
  


Locating Array Elements

   <source lang="html4strict">

//in_array() function determines whether or not an element exists in an array, returning true if it does, //and false otherwise. //Its syntax is: bool in_array(mixed element, array array) <?

   $languages = array ("English", "Gaelic", "Spanish");
   $exists = in_array("Russian", $languages); 
   print $exists;
   print "
"; $exists = in_array("English", $languages); print $exists;

?>

 </source>