0%

源码

x

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

require __DIR__ . '/bootstrap/app.php';

$arr = [
[
'name' => 'John',
'age' => 23
],
[
'name' => 'Neo',
'age' => 25
],
[
'name' => 'John',
'age' => 24
]
];

$v = collect($arr)->mapToDictionary(function ($v) {
// 主要作用: 可以自定义每一项的 key value
return [$v['name'] => $v];
})->toArray();

// $v 与 $v1 相等
$v1 = collect($arr)->groupBy('name')->toArray();

dd($v);

输出

x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$collection = collect(range(1, 9));

$chunks = $collection->chunk(2);

$labeld = $chunks->mapSpread(function ($odd, $even) {
return "Odd: {$odd} Even: {$even}";
});

$numbers = collect([3, 5, 7]);
$words = collect(['three', 'five', 'seven']);

$results = $numbers->zip($words)
->mapSpread(function ($number, $word) {
return $number . '=' . $word;
});

dd($labeld->toArray(), $results->toArray());
x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (! function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* @param mixed $args
* @return void
*/
function dd(...$args)
{
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
http_response_code(500);

foreach ($args as $x) {
(new Illuminate\Support\Debug\Dumper)->dump($x);
}

die(1);
}
}