数组 函数 业 ,精于勤 荒于嬉.
- 数组 函数 array_sum 对数组中所有值求和
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $a = array(2, 4, 6, 8); echo "sum(a) = " . array_sum($a) . "\n"; $b = array("a" => 1.2, "b" => 2.3, "c" => 3.4); echo "sum(b) = " . array_sum($b) . "\n"; ?>
- 数组 函数 array_udiff_assoc 带索引检查计算数组的差集,用回调函数比较数据
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php class cr { private $priv_member; function cr($val) { $this->priv_member = $val; } static function comp_func_cr($a, $b) { if ($a->priv_member === $b->priv_member) return 0; return ($a->priv_member > $b->priv_member)? 1:-1; } } $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),); $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),); $result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr")); print_r($result); ?>
- 数组 函数 array_udiff_uassoc 带索引检查计算数组的差集,用回调函数比较数据和索引
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php class cr { private $priv_member; function cr($val) { $this->priv_member = $val; } static function comp_func_cr($a, $b) { if ($a->priv_member === $b->priv_member) return 0; return ($a->priv_member > $b->priv_member)? 1:-1; } static function comp_func_key($a, $b) { if ($a === $b) return 0; return ($a > $b)? 1:-1; } } $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),); $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),); $result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key")); print_r($result); ?>
- 数组 函数 array_udiff 用回调函数比较数据来计算数组的差集
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php // Arrays to compare$array1 = array(new stdclass, new stdclass, new stdclass, new stdclass, ); $array2 = array( new stdclass, new stdclass, ); // Set some properties for each object$array1[0]->width = 11; $array1[0]->height = 3; $array1[1]->width = 7; $array1[1]->height = 1; $array1[2]->width = 2; $array1[2]->height = 9; $array1[3]->width = 5; $array1[3]->height = 7; $array2[0]->width = 7; $array2[0]->height = 5; $array2[1]->width = 9; $array2[1]->height = 2; function compare_by_area($a, $b) { $areaA = $a->width * $a->height; $areaB = $b->width * $b->height; if ($areaA < $areaB) { return -1; } elseif ($areaA > $areaB) { return 1; } else { return 0; } } print_r(array_udiff($array1, $array2, 'compare_by_area')); ?>
示例2
<?php class MyCalendar { public $free = array(); public $booked = array(); public function __construct($week = 'now') { $start = new DateTime($week); $start->modify('Monday this week midnight'); $end = clone $start; $end->modify('Friday this week midnight'); $interval = new DateInterval('P1D'); foreach (new DatePeriod($start, $interval, $end) as $freeTime) { $this->free[] = $freeTime; } } public function bookAppointment(DateTime $date, $note) { $this->booked[] = array('date' => $date->modify('midnight'), 'note' => $note); } public function checkAvailability() { return array_udiff($this->free, $this->booked, array($this, 'customCompare')); } public function customCompare($free, $booked) { if (is_array($free)) $a = $free['date']; else $a = $free; if (is_array($booked)) $b = $booked['date']; else $b = $booked; if ($a == $b) { return 0; } elseif ($a > $b) { return 1; } else { return -1; } } } // Create a calendar for weekly appointments$myCalendar = new MyCalendar; // Book some appointments for this week$myCalendar->bookAppointment(new DateTime('Monday this week'), "Cleaning GoogleGuy's apartment."); $myCalendar->bookAppointment(new DateTime('Wednesday this week'), "Going on a snowboarding trip."); $myCalendar->bookAppointment(new DateTime('Friday this week'), "Fixing buggy code."); // Check availability of days by comparing $booked dates against $free datesecho "I'm available on the following days this week...\n\n"; foreach ($myCalendar->checkAvailability() as $free) { echo $free->format('l'), "\n"; } echo "\n\n"; echo "I'm busy on the following days this week...\n\n"; foreach ($myCalendar->booked as $booked) { echo $booked['date']->format('l'), ": ", $booked['note'], "\n"; } ?>
- 数组 函数 array_uintersect_assoc 带索引检查计算数组的交集,用回调函数比较数据
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_uintersect_assoc($array1, $array2, "strcasecmp")); ?>
- 数组 函数 array_uintersect_uassoc 带索引检查计算数组的交集,用单独的回调函数比较数据和索引
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp")); ?>
- 数组 函数 array_uintersect 计算数组的交集,用回调函数比较数据
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_uintersect($array1, $array2, "strcasecmp")); ?>
- 数组 函数 array_unique 移除数组中重复的值
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?>
示例2
<?php $input = array(4, "4", "3", 4, 3, "3"); $result = array_unique($input); var_dump($result); ?>
- 数组 函数 array_unshift 在数组开头插入一个或多个单元
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $queue = array("orange", "banana"); array_unshift($queue, "apple", "raspberry"); print_r($queue); ?>
- 数组 函数 array_values 返回数组中所有的值
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $array = array("size" => "XL", "color" => "gold"); print_r(array_values($array)); ?>
- 数组 函数 array_walk_recursive 对数组中的每个成员递归地应用用户函数
-
发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数
-
示例1
<?php $sweet = array('a' => 'apple', 'b' => 'banana'); $fruits = array('sweet' => $sweet, 'sour' => 'lemon'); function test_print($item, $key){ echo "$key holds $item\n"; } array_walk_recursive($fruits, 'test_print'); ?>
- 数组 函数 array_walk 使用用户自定义函数对数组中的每个元素做回调处理
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_alter(&$item1, $key, $prefix){ $item1 = "$prefix: $item1"; } function test_print($item2, $key){ echo "$key. $item2<br />\n"; } echo "Before ...:\n"; array_walk($fruits, 'test_print'); array_walk($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; array_walk($fruits, 'test_print'); ?>
- 数组 函数 array 新建一个数组
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third")); ?>
示例2
<?php $array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13); print_r($array); ?>
示例3
<?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?>
示例4
<?php $foo = array('bar' => 'baz'); echo "Hello { $foo['bar']} !"; // Hello baz!?>
- 数组 函数 arsort 对数组进行降向排序并保持索引关系
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); arsort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?>
- 数组 函数 asort 对数组进行升序排序并保持索引关系
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?>
- 数组 函数 compact 建立一个数组,包括变量名和它们的值
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $city = "San Francisco"; $state = "CA"; $event = "SIGGRAPH"; $location_vars = array("city", "state"); $result = compact("event", $location_vars); print_r($result); ?>
- 数组 函数 count 计算数组中的单元数目,或对象中的属性个数
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $a[0] = 1; $a[1] = 3; $a[2] = 5; var_dump(count($a)); $b[0] = 7; $b[5] = 9; $b[10] = 11; var_dump(count($b)); ?>
示例2
<?php $b[0] = 7; $b[5] = 9; $b[10] = 11; var_dump(count($b)); var_dump(count(null)); var_dump(count(false)); ?>
示例3
<?php $food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea')); // recursive countecho count($food, COUNT_RECURSIVE); // output 8// normal countecho count($food); // output 2?>
- 数组 函数 current 返回数组中的当前值
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $transport = array('foot', 'bike', 'car', 'plane'); $mode = current($transport); // $mode = 'foot'; $mode = next($transport); // $mode = 'bike'; $mode = current($transport); // $mode = 'bike'; $mode = prev($transport); // $mode = 'foot'; $mode = end($transport); // $mode = 'plane'; $mode = current($transport); // $mode = 'plane'; $arr = array(); var_dump(current($arr)); // bool(false)$arr = array(array()); var_dump(current($arr)); // array(0) { } ?>
- 数组 函数 each 返回数组中当前的键/值对并将数组指针向前移动一步
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each($foo); print_r($bar); ?>
示例2
<?php $foo = array("Robert" => "Bob", "Seppo" => "Sepi"); $bar = each($foo); print_r($bar); ?>
示例3
<?php $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); reset($fruit); while (list($key, $val) = each($fruit)) { echo "$key => $val\n"; } ?>
- 数组 函数 end 将数组的内部指针指向最后一个单元
-
发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数
-
示例1
<?php $fruits = array('apple', 'banana', 'cranberry'); echo end($fruits); // cranberry?>
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)