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

数组 函数 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');
?>

阅读全文 »

数组 函数 extract 从数组中将变量导入到当前的符号表

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

      示例1
<?php 
/* 假定 $var_array 是 wddx_deserialize 返回的数组*/
$size = "large";
$var_array = array("color" => "blue",                   "size"  => "medium",                   "shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>

阅读全文 »

数组 函数 key_exists 别名 array_key_exists()

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

key_exists

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

key_exists别名 array_key_exists()

说明

此函数是该函数的别名: array_key_exists().

阅读全文 »

数组 函数 key 从关联数组中取得键名

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

      示例1
<?php 
$array = array(    'fruit1' => 'apple',    'fruit2' => 'orange',    'fruit3' => 'grape',    'fruit4' => 'apple',    'fruit5' => 'apple');
// this cycle echoes all associative array// key where value equals "apple"while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array), "\n";
    }
    next($array);
}
?>

阅读全文 »

数组 函数 end 将数组的内部指针指向最后一个单元

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

      示例1
<?php 
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits);
 // cranberry?>

阅读全文 »

数组 函数 krsort 对数组按照键名逆向排序

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

      示例1
<?php 
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

阅读全文 »

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

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

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

阅读全文 »

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

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

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

阅读全文 »

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

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

      示例1
<?php 
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
var_dump($input);
?>

      示例2
<?php 
// 添加两个新元素到 $inputarray_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));
// 移除 $input 中的最后一个元素array_pop($input);
array_splice($input, -1);
// 移除  $input 中第一个元素array_shift($input);
array_splice($input, 0, 1);
// 在 $input 的开头插入一个元素array_unshift($input, $x, $y);
array_splice($input, 0, 0, array($x, $y));
// 在 $input  的索引  $x 处替换值$input[$x] = $y;
 // 对于键名和偏移量等值的数组array_splice($input, $x, 1, $y);
?>

阅读全文 »

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

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

      示例1
<?php 
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2);
      // returns "c", "d", and "e"$output = array_slice($input, -2, 1);
  // returns "d"$output = array_slice($input, 0, 3);
   // returns "a", "b", and "c"// note the differences in the array keysprint_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

      示例2
<?php 
$input = array(1 => "a", "b", "c", "d", "e");
print_r(array_slice($input, 1, 2));
?>

      示例3
<?php 
$ar = array('a'=>'apple', 'b'=>'banana', '42'=>'pear', 'd'=>'orange');
print_r(array_slice($ar, 0, 3));
print_r(array_slice($ar, 0, 3, true));
?>

阅读全文 »

数组 函数 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_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_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_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_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_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_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_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_unshift 在数组开头插入一个或多个单元

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

      示例1
<?php 
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>

阅读全文 »

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

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

      示例1
<?php 
$array[] = $var;
?>

      示例2
<?php 
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>

阅读全文 »

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