无需言 做自己 业 ,精于勤 荒于嬉.

数组 函数 ksort 对数组根据键名升序排序

发表日期:2021-07-01 08:57:09 | 来源: | 分类:数组 函数

      示例1
1<?php
2$fruits array("d"=>"lemon""a"=>"orange""b"=>"banana""c"=>"apple");
3ksort($fruits);
4foreach ($fruits as $key => $val) {
5    echo "$key = $val\n";
6}
7?>

阅读全文 »

数组 函数 array_product 计算数组中所有值的乘积

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$a array(2, 4, 6, 8);
3echo "product(a) = " array_product($a) . "\n";
4echo "product(array()) = " array_product(array()) . "\n";
5?>

阅读全文 »

数组 函数 array_splice 去掉数组中的某一部分并用其它值取代

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
01<?php
02$input array("red""green""blue""yellow");
03array_splice($input, 2);
04var_dump($input);
05$input array("red""green""blue""yellow");
06array_splice($input, 1, -1);
07var_dump($input);
08$input array("red""green""blue""yellow");
09array_splice($input, 1, count($input), "orange");
10var_dump($input);
11$input array("red""green""blue""yellow");
12array_splice($input, -1, 1, array("black""maroon"));
13var_dump($input);
14?>
      示例2
01<?php
02// 添加两个新元素到 $inputarray_push($input, $x, $y);
03array_splice($inputcount($input), 0, array($x$y));
04// 移除 $input 中的最后一个元素array_pop($input);
05array_splice($input, -1);
06// 移除  $input 中第一个元素array_shift($input);
07array_splice($input, 0, 1);
08// 在 $input 的开头插入一个元素array_unshift($input, $x, $y);
09array_splice($input, 0, 0, array($x$y));
10// 在 $input  的索引  $x 处替换值$input[$x] = $y;
11 // 对于键名和偏移量等值的数组array_splice($input, $x, 1, $y);
12?>

阅读全文 »

数组 函数 array_slice 从数组中取出一段

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$input array("a""b""c""d""e");
3$output array_slice($input, 2);
4      // returns "c", "d", and "e"$output = array_slice($input, -2, 1);
5  // returns "d"$output = array_slice($input, 0, 3);
6   // returns "a", "b", and "c"// note the differences in the array keysprint_r(array_slice($input, 2, -1));
7print_r(array_slice($input, 2, -1, true));
8?>
      示例2
1<?php
2$input array(1 => "a""b""c""d""e");
3print_r(array_slice($input, 1, 2));
4?>
      示例3
1<?php
2$ar array('a'=>'apple''b'=>'banana''42'=>'pear''d'=>'orange');
3print_r(array_slice($ar, 0, 3));
4print_r(array_slice($ar, 0, 3, true));
5?>

阅读全文 »

数组 函数 array_sum 对数组中所有值求和

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$a array(2, 4, 6, 8);
3echo "sum(a) = " array_sum($a) . "\n";
4$b array("a" => 1.2, "b" => 2.3, "c" => 3.4);
5echo "sum(b) = " array_sum($b) . "\n";
6?>

阅读全文 »

数组 函数 array_udiff_uassoc 带索引检查计算数组的差集,用回调函数比较数据和索引

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
01<?php
02class cr {
03    private $priv_member;
04    function cr($val)    {
05        $this->priv_member = $val;
06    }
07    static function comp_func_cr($a$b)    {
08        if ($a->priv_member === $b->priv_member) return 0;
09        return ($a->priv_member > $b->priv_member)? 1:-1;
10    }
11    static function comp_func_key($a$b)    {
12        if ($a === $breturn 0;
13        return ($a $b)? 1:-1;
14    }
15}
16$a array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
17$b array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
18$result array_udiff_uassoc($a$barray("cr""comp_func_cr"), array("cr""comp_func_key"));
19print_r($result);
20?>

阅读全文 »

数组 函数 array_udiff_assoc 带索引检查计算数组的差集,用回调函数比较数据

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
01<?php
02class cr {
03    private $priv_member;
04    function cr($val)    {
05        $this->priv_member = $val;
06    }
07    static function comp_func_cr($a$b)    {
08        if ($a->priv_member === $b->priv_member) return 0;
09        return ($a->priv_member > $b->priv_member)? 1:-1;
10    }
11}
12$a array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
13$b array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
14$result array_udiff_assoc($a$barray("cr""comp_func_cr"));
15print_r($result);
16?>

阅读全文 »

数组 函数 array_uintersect_uassoc 带索引检查计算数组的交集,用单独的回调函数比较数据和索引

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array1 array("a" => "green""b" => "brown""c" => "blue""red");
3$array2 array("a" => "GREEN""B" => "brown""yellow""red");
4print_r(array_uintersect_uassoc($array1$array2"strcasecmp""strcasecmp"));
5?>

阅读全文 »

数组 函数 array_uintersect_assoc 带索引检查计算数组的交集,用回调函数比较数据

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array1 array("a" => "green""b" => "brown""c" => "blue""red");
3$array2 array("a" => "GREEN""B" => "brown""yellow""red");
4print_r(array_uintersect_assoc($array1$array2"strcasecmp"));
5?>

阅读全文 »

数组 函数 array_udiff 用回调函数比较数据来计算数组的差集

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
01<?php
02// Arrays to compare$array1 = array(new stdclass, new stdclass,                new stdclass, new stdclass,               );
03$array2 array(                new stdclass, new stdclass,               );
04// Set some properties for each object$array1[0]->width = 11;
05 $array1[0]->height = 3;
06$array1[1]->width = 7;
07  $array1[1]->height = 1;
08$array1[2]->width = 2;
09  $array1[2]->height = 9;
10$array1[3]->width = 5;
11  $array1[3]->height = 7;
12$array2[0]->width = 7;
13  $array2[0]->height = 5;
14$array2[1]->width = 9;
15  $array2[1]->height = 2;
16function compare_by_area($a$b) {
17    $areaA $a->width * $a->height;
18    $areaB $b->width * $b->height;
19        if ($areaA $areaB) {
20        return -1;
21    }
22 elseif ($areaA $areaB) {
23        return 1;
24    }
25 else {
26        return 0;
27    }
28}
29print_r(array_udiff($array1$array2'compare_by_area'));
30?>
      示例2
01<?php
02class MyCalendar {
03    public $free array();
04    public $booked array();
05    public function __construct($week 'now') {
06        $start new DateTime($week);
07        $start->modify('Monday this week midnight');
08        $end = clone $start;
09        $end->modify('Friday this week midnight');
10        $interval new DateInterval('P1D');
11        foreach (new DatePeriod($start$interval$endas $freeTime) {
12            $this->free[] = $freeTime;
13        }
14    }
15    public function bookAppointment(DateTime $date$note) {
16        $this->booked[] = array('date' => $date->modify('midnight'), 'note' => $note);
17    }
18    public function checkAvailability() {
19        return array_udiff($this->free, $this->booked, array($this'customCompare'));
20    }
21        public function customCompare($free$booked) {
22        if (is_array($free)) $a $free['date'];
23        else $a $free;
24        if (is_array($booked)) $b $booked['date'];
25        else $b $booked;
26        if ($a == $b) {
27            return 0;
28        }
29 elseif ($a $b) {
30            return 1;
31        }
32 else {
33            return -1;
34        }
35    }
36}
37// Create a calendar for weekly appointments$myCalendar = new MyCalendar;
38// Book some appointments for this week$myCalendar->bookAppointment(new DateTime('Monday this week'), "Cleaning GoogleGuy's apartment.");
39$myCalendar->bookAppointment(new DateTime('Wednesday this week'), "Going on a snowboarding trip.");
40$myCalendar->bookAppointment(new DateTime('Friday this week'), "Fixing buggy code.");
41// Check availability of days by comparing $booked dates against $free datesecho "I'm available on the following days this week...\n\n";
42foreach ($myCalendar->checkAvailability() as $free) {
43    echo $free->format('l'), "\n";
44 }
45echo "\n\n";
46echo "I'm busy on the following days this week...\n\n";
47foreach ($myCalendar->booked as $booked) {
48    echo $booked['date']->format('l'), ": "$booked['note'], "\n";
49 }
50?>

阅读全文 »

数组 函数 array_unique 移除数组中重复的值

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$input array("a" => "green""red""b" => "green""blue""red");
3$result array_unique($input);
4print_r($result);
5?>
      示例2
1<?php
2$input array(4, "4""3", 4, 3, "3");
3$result array_unique($input);
4var_dump($result);
5?>

阅读全文 »

数组 函数 array_uintersect 计算数组的交集,用回调函数比较数据

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array1 array("a" => "green""b" => "brown""c" => "blue""red");
3$array2 array("a" => "GREEN""B" => "brown""yellow""red");
4print_r(array_uintersect($array1$array2"strcasecmp"));
5?>

阅读全文 »

数组 函数 array_unshift 在数组开头插入一个或多个单元

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$queue array("orange""banana");
3array_unshift($queue"apple""raspberry");
4print_r($queue);
5?>

阅读全文 »

数组 函数 array_push 将一个或多个单元压入数组的末尾(入栈)

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array[] = $var;
3?>
      示例2
1<?php
2$stack array("orange""banana");
3array_push($stack"apple""raspberry");
4print_r($stack);
5?>

阅读全文 »

数组 函数 array_shift 将数组开头的单元移出数组

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$stack array("orange""banana""apple""raspberry");
3$fruit array_shift($stack);
4print_r($stack);
5?>

阅读全文 »

数组 函数 array_walk_recursive 对数组中的每个成员递归地应用用户函数

发表日期:2021-07-01 08:57:08 | 来源: | 分类:数组 函数

      示例1
1<?php
2$sweet array('a' => 'apple''b' => 'banana');
3$fruits array('sweet' => $sweet'sour' => 'lemon');
4function test_print($item$key){
5    echo "$key holds $item\n";
6}
7array_walk_recursive($fruits'test_print');
8?>

阅读全文 »

数组 函数 array_key_exists 检查数组里是否有指定的键名或索引

发表日期:2021-07-01 08:57:07 | 来源: | 分类:数组 函数

      示例1
1<?php
2$search_array array('first' => 1, 'second' => 4);
3if (array_key_exists('first'$search_array)) {
4    echo "The 'first' element is in the array";
5}
6?>
      示例2
1<?php
2$search_array array('first' => null, 'second' => 4);
3// returns falseisset($search_array['first']);
4// returns truearray_key_exists('first', $search_array);
5?>

阅读全文 »

数组 函数 array_key_first 获取指定数组的第一个键值

发表日期:2021-07-01 08:57:07 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array = ['a' => 1, 'b' => 2, 'c' => 3];
3$firstKey = array_key_first($array);
4var_dump($firstKey);
5?>
      示例2
01<?php
02if (!function_exists('array_key_first')) {
03    function array_key_first(array $arr) {
04        foreach($arr as $key => $unused) {
05            return $key;
06        }
07        return NULL;
08    }
09}
10?>

阅读全文 »

数组 函数 array_keys 返回数组中部分的或所有的键名

发表日期:2021-07-01 08:57:07 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array array(0 => 100, "color" => "red");
3print_r(array_keys($array));
4$array array("blue""red""green""blue""blue");
5print_r(array_keys($array"blue"));
6$array array("color" => array("blue""red""green"),               "size"  => array("small""medium""large"));
7print_r(array_keys($array));
8?>

阅读全文 »

数组 函数 array_diff_key 使用键名比较计算数组的差集

发表日期:2021-07-01 08:57:07 | 来源: | 分类:数组 函数

      示例1
1<?php
2$array1 array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
3$array2 array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
4var_dump(array_diff_key($array1$array2));
5?>

阅读全文 »

全部博文(1583)
集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。