PHP头条
热点:

实用函数

有相当多的实用函数作用于数组,参见数组函数库一节。

注: unset() 函数允许取消一个数组中的键名。要注意数组将不会重建索引。

 'one', 2 => 'two', 3 => 'three' );
unset( $a[2] );
/* 将产生一个数组,定义为
   $a = array( 1=>'one', 3=>'three');
   而不是
   $a = array( 1 => 'one', 2 => 'three');
*/
$b = array_values($a);
// Now b is array(1 => 'one', 2 =>'three')
?>  

foreach 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。

数组做什么和不做什么

为什么 $foo[bar] 错了?

应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么 $foo[bar] 错了呢?你可能在老的脚本中见过如下语法

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量bar)而不是字符串'bar'-注意引号),而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 PHP 自动将裸字符串没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为 bar,PHP 将把它替代为 'bar' 并使用之。

注: 这并不意味着总是给键名加上引号。用不着给键名为常量 或 变量 的加上引号,否则会使 PHP 不能解析它们。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
   echo "\nChecking $i: \n";
   echo "Bad: " . $array['$i'] . "\n";
   echo "Good: " . $array[$i] . "\n";
   echo "Bad: {$array['$i']}\n";
   echo "Good: {$array[$i]}\n";
}
?>

注: 上面例子输出为:

Checking 0:
Notice: Undefined index:  $i in /path/to/script.html on line 9
Bad:
Good: 1
Notice: Undefined index:  $i in /path/to/script.html on line 11
Bad:
Good: 1

Checking 1:
Notice: Undefined index:  $i in /path/to/script.html on line 9
Bad:
Good: 2
Notice: Undefined index:  $i in /path/to/script.html on line 11
Bad:
Good: 2

演示此效应的更多例子:

 'apple', 'veggie' => 'carrot');

// 正确
print $arr['fruit'];  // apple
print $arr['veggie']; // carrot

// 不正确。This works but also throws a PHP error of
// level E_NOTICE because of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit];    // apple

// Let's define a constant to demonstrate what's going on.  We
// will assign value 'veggie' to a constant named fruit.
define('fruit','veggie');

// Notice the difference now
print $arr['fruit'];  // apple
print $arr[fruit];    // carrot

// The following is okay as it's inside a string.  Constants are not
// looked for within strings so no E_NOTICE error here
print "Hello $arr[fruit]";      // Hello apple

// With one exception, braces surrounding arrays within strings
// allows constants to be looked for
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}";  // Hello apple

// This will not work, results in a parse error such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using autoglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?> 
</TD>

当打开 error_reporting() 来显示 E_NOTICE 级别的错误例如将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。

和在语法一节中规定的一样,在方括号“[”和“]”)之间必须有一个表达式。这意味着你可以这样写:

<?php
echo $arr[somefunc($bar)];
?>

这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量,你可能之前已经见过 E_*

<?php
$error_descriptions[E_ERROR]  = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE]  = "This is just an informal notice";
?>

注意 E_ERROR 也是个合法的标识符,就和第一个例子中的 bar 一样。但是上一个例子实际上和如下写法是一样的:

<?php
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?> 

因为 E_ERROR 等于 1,等等。

如同我们在以上例子中解释的那样,$foo[bar] 起作用但其实是错误的。它起作用是因为根据语法的预期,bar 被当成了一个常量表达式。然而,在这个例子中不存在名为 bar 的常量。PHP 就假定你指的是字面上的 bar,也就是字符串 "bar",但你忘记写引号了。

那么为什么这样做不好?

在未来的某一时刻,PHP 开发小组可能会想新增一个常量或者关键字,或者您可能希望在以后在您的程序中引入新的常量,那你就有麻烦了。例如你已经不能这样用 empty 和 default 这两个词了,因为他们是保留字。

注: 重申一次,在双引号字符串中,不给索引加上引号是合法的因此 "$foo[bar]"是合法的。至于为什么参见以上的例子和字符串中的变量解析中的解释。

转换为数组

对于任何的类型:整型、浮点、字符串、布尔和资源,如果您将一个值转换为数组,您将得到一个仅有一个元素的数组其下标为 0),该元素即为此标量的值。

如果您将一个对象转换成一个数组,您所得到的数组的元素为该对象的属性成员变量),其键名为成员变量名。

如果您将一个 NULL 值转换成数组,您将得到一个空数组。

例子

PHP 中的数组类型有非常多的用途,因此这里有一些例子展示数组的完整威力。 

 'red',
           'taste' => 'sweet',
           'shape' => 'round',
           'name'  => 'apple',
                       4        // key will be 0
         );

// is completely equivalent with
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name']  = 'apple';
$a[]        = 4;        // key will be 0

$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),
// or simply array('a', 'b', 'c')
?> 

例子 11-4. 使用 array()

 4,
             'OS'        => 'Linux',
             'lang'      => 'english',
             'short_tags' => true
           );

// strictly numerical keys
$array = array( 7,
               8,
               0,
               156,
               -10
             );
// this is the same as array(0 => 7, 1 => 8, ...)

$switching = array(        10, // key = 0
                   5    =>  6,
                   3    =>  7,
                   'a'  =>  4,
                           11, // key = 6 (maximum of integer-indices was 5)
                   '8'  =>  2, // key = 8 (integer!)
                   '02' => 77, // key = '02'
                   0    => 12  // the value 10 will be overwritten by 12
                 );
// empty array
$empty = array();
?> 

例子 11-5. 集合

<?php
$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $color) {
   echo "Do you like $color?\n";
}

/* output:
Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
*/
?>

注意目前不可能在这样一个循环中直接改变数组的值。可以改变的例子如下:
例子 11-6. 集合

 $color) {
   // won't work:
   //$color = strtoupper($color);

   //works:
   $colors[$key] = strtoupper($color);
}
print_r($colors);

/* output:
Array
(
   [0] => RED
   [1] => BLUE
   [2] => GREEN
   [3] => YELLOW
)
*/
?>

本例产生一个基于一的数组。 例子 11-7. 基于一的数组

 'January', 'February', 'March');
print_r($firstquarter);
/* output:
Array
(
   [1] => 'January'
   [2] => 'February'
   [3] => 'March'
)
*/
?> 


www.phpzy.comtrue/php/19000.htmlTechArticle实用函数 有相当多的实用函数作用于数组,参见数组函数库一节。 注: unset() 函数允许取消一个数组中的键名。要注意数组将不会重建索引。 'one', 2 => '...

相关文章

PHP之友评论

今天推荐