略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Threaded::merge

2024-11-13

Threaded::merge

(PECL pthreads >= 2.0.0)

Threaded::merge操作

说明

publicThreaded::merge(mixed $from, bool $overwrite = ?): bool

将数据合并到当前对象

参数

from

要合并的数据

overwrite

如果现有对象已经存在同键的数据,是否覆盖。默认为 true

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 合并数据到对象的属性表

<?php
$array 
= [];

while (
count($array) < 10)
    
$array[] = count($array);

$stdClass = new stdClass();
$stdClass->foo "foo";
$stdClass->bar "bar";
$stdClass->baz "baz";

$safe = new Threaded();
$safe->merge($array);
$safe->merge($stdClass);

var_dump($safe);
?>

以上例程会输出:

object(Threaded)#2 (13) {
  ["0"]=>
  int(0)
  ["1"]=>
  int(1)
  ["2"]=>
  int(2)
  ["3"]=>
  int(3)
  ["4"]=>
  int(4)
  ["5"]=>
  int(5)
  ["6"]=>
  int(6)
  ["7"]=>
  int(7)
  ["8"]=>
  int(8)
  ["9"]=>
  int(9)
  ["foo"]=>
  string(3) "foo"
  ["bar"]=>
  string(3) "bar"
  ["baz"]=>
  string(3) "baz"
}
add a noteadd a note

User Contributed Notes 1 note

up
0
cottton
6 years ago
The example output is different on my side:
<?php
/*
object(Threaded)#2 (11) {
  ["0"]=>
  int(0)
  ["1"]=>
  int(1)
  ["2"]=>
  int(2)
  ["3"]=>
  int(3)
  ["4"]=>
  int(4)
  ["5"]=>
  int(5)
  ["6"]=>
  int(6)
  ["7"]=>
  int(7)
  ["8"]=>
  int(8)
  ["9"]=>
  int(9)
  ["foo"]=>
  string(3) "bar"
}
*/
?>
for
windows using
PHP_VERSION 5.6.11
php_pthreads-2.0.10-5.6-ts-vc11-x86.zip @ http://windows.php.net/downloads/pecl/releases/pthreads/2.0.10/

and

linux using
PHP_VERSION 5.6.10-1~dotdeb+zts+7.3

So $stdClass wont be merged.

官方地址:https://www.php.net/manual/en/threaded.merge.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3