在php数组中有三种类型:数值数组 带有数字 ID 键的数组 关联数组 数组中的每个 ID 键关联一个值 多维数组 包含一个或多个数组的数组,好了现在来看我的笔记吧.
实例代码如下:
- header('Content-Type: text/html; charset=utf-8');
- echo '<pre>';
- function var_array($array)
- {
- echo '<pre>';
- var_dump($array);
- echo '</pre>';
- }
- function printr($array)
- {
- echo '<pre>';
- print_r($array);
- echo '</pre>';
- }
- function getArr($sNum, $eNum=1, $step=1)
- {
- $arr = range($sNum, $eNum, $step);
- $reArr = array();
- foreach($arr as $v)
- {
- $reArr[$v] = rand(0,10);
- }
- unset($arr);
- return $reArr;
- }
-
-
-
-
-
- $expArr = array(
- 'fiRsT' => '1',
- 'sEcoNd' => '2',
- 'ThIrd' => array(
- 'HuiMa' => '3',
- 'nengZhuaNma' => '5',
- )
- );
- printr(array_change_key_case($expArr));
- printr(array_change_key_case($expArr['ThIrd'], CASE_UPPER));
-
- echo '<br/><hr/><br/>';
-
-
-
- $expArr = array('4','2','6','d','2');
- printr(array_chunk($expArr, 3));
-
- echo '<br/><hr/><br/>';
-
-
-
- $expKey = array('g', 'd', 't');
- $expVal = array('5', '8', '7');
- printr(array_combine($expKey, $expVal));
-
- echo '<br/><hr/><br/>';
-
-
-
- $array = array('v1'=>'265', 'v2'=>'352', 'v3'=>'265', 'v4'=>'349', 'v5'=>'265');
- printr(array_count_values($array));
-
- echo '<br/><hr/><br/>';
-
-
-
- $arr1 = array('v4'=>458, 'gren', 'b5', 'a5');
- $arr2 = array('v4'=>598, 'red', 'a5', 'c4');
- printr(array_diff($arr1, $arr2));
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
- printr(array_fill(2, 5, 'value'));
-
- echo '<br/><hr/><br/>';
-
-
-
- $arrKeys = array('45', 'd', 'b', 'c');
- printr(array_fill_keys($arrKeys, 'value'));
- echo '<br/><hr/><br/>';
-
-
-
-
- function cb($val)
- {
- return $val%2 == 0;
- }
- $array = array('k1'=>3, 'k2'=>5,'k4'=>54654, 'k5'=>8794, 8945, 32549564);
- printr($array, 'cb');
-
-
- unset($array);
- echo '<br/><hr/><br/>';
-
-
-
- $arr = array('k1'=>'v1', 'k2'=>'v2', 'k3'=>'v4', 'k4'=>'v4', 'k5'=>'v5');
- printr(array_flip($arr));
- unset($arr);
- echo '<br/><hr/><br/>';
-
-
-
- $array = array('cb' => 234, 'dv'=>45, 'one'=>897);
- if(array_key_exists('one', $array))
- echo '存在这个数组中';
- else
- echo '不存在';
- echo '<br/><hr/><br/>';
-
-
-
- $array = getArr(4, 10);
- printr(array_keys($array));
- printr(array_keys($array, '5'));
- unset($array);
-
- echo '<br/><hr/><br/>';
-
- echo 'array_map:';
-
-
-
- function mapCb($n)
- {
- return $n*$n*$n;
- }
- $array = getArr(4, 15);
- printr(array_map('mapCb', $array));
- echo '<br/><hr/><br/>';
-
-
-
- $arr1 = getArr(1, 5);
- $arr2 = getArr(5, 10);
- printr(array_merge($arr1, $arr2));
-
- echo '<br/><hr/><br/>';
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
- $arr = array(4,5,5);
- echo array_product($arr);
- echo '<br/><hr/><br/>';
-
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
-
- $arr = getArr(5, 15);
- printr(array_rand($arr, 4));
- echo '<br/><hr/><br/>';
-
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
-
-
-
- $base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
- $replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
- printr(array_replace($base, $replacements));
- echo '<br/><hr/><br/>';
-
-
-
- $base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
- $replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
- printr(array_replace_recursive($base, $replacements));
- echo '<br/><hr/><br/>';
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
-
- $input = array("a", "b", "c", "d", "e");
- $output = array_slice($input, 2);
- $output = array_slice($input, -2, 1);
- $output = array_slice($input, 0, 3);
-
- printr(array_slice($input, 2, -1));
- printr(array_slice($input, 2, -1, true));
- echo '<br/><hr/><br/>';
-
-
-
- unset($arr);
- echo '<br/><hr/><br/>';
-
-
-
-
- $arr = array(
- 45,56, 'a', 'b'=>getArr(1, 2),
- );
- printr($arr);
- echo 'array_sum($arr)',array_sum($arr);
- echo '<br/><hr/><br/>';
-
-
-
- $arr = array(
- 'k1'=>45,'k2'=>56, 'k3'=>'a', 'b'=>getArr(1, 2),
- );
- printr(array_values($arr));
- echo '<br/><hr/><br/>';
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
-
-
- $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
- function test_alter(&$item1, $key, $prefix)
- {
- $item1 = "$prefix: $item1";
- }
- printr(array_walk($fruits, 'test_print'));
- array_walk($fruits, 'test_alter', 'fruit');
- echo '<br/><hr/><br/>';
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
- echo '<br/><hr/><br/>';
-
-
-
- 数组循环
- 实例代码如下:
- foreach ($array as $value) {
- echo $value;
- }
- while (list($key) = each($array)) {
- echo $array[$key];
- }
- foreach ($array as $value) {
- echo $value;
- }
- while (list($key) = each($array)) {
- echo $array[$key];
- }
在循环里进行的是数组“写”操作,则while比foreach快:
实例代码如下:
- foreach ($array as $key => $value) {
- echo $array[$key] = $value . '...';
- }
- while (list($key) = each($array)) {
- $array[$key] = $array[$key] . '...';
- }
- foreach ($array as $key => $value) {
- echo $array[$key] = $value . '...';
- }
- while (list($key) = each($array)) {
- $array[$key] = $array[$key] . '...';
- }
总结:通常认为,foreach涉及到值复制,一定会比while慢,但实际上,如果仅仅是在循环里进行数组的读操作,那么foreach是很快的,这是因为PHP采用的复制机制是“引用复制,写时拷贝”,这样看来,foreach的高效读操作就不难理解了.
另外,既然foreach不适合处理数组写操作,那么我们可以得出一个结论,多数情况下,类似foreach ($array as $key => $value)形式的代码都应该被替换成while (list($key) = each($array)).