You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.2 KiB
68 lines
1.2 KiB
<?php
|
|
|
|
$data = [1, 2, 3, 4, 5];
|
|
|
|
$values=array_map(
|
|
fn($item) : int => $item**2,
|
|
$data
|
|
);
|
|
|
|
print_r($values);
|
|
|
|
$poem = [
|
|
'the horse and the hound and the horn that belonged to',
|
|
'the farmer sowing his corn that kept',
|
|
'the rooster that crowed in the morn that woke',
|
|
'the priest all shaven and shorn that married',
|
|
'the man all tattered and torn that kissed',
|
|
'the maiden all forlorn that milked',
|
|
'the cow with the crumpled horn that tossed',
|
|
'the dog that worried',
|
|
'the cat that killed',
|
|
'the rat that ate',
|
|
'the malt that lay in',
|
|
'the house that Jack built',
|
|
];
|
|
|
|
echo '<br>';
|
|
$parts=array_slice($poem, -3, 2);
|
|
//print_r(implode("\n ", $parts));
|
|
|
|
$reduce=array_reduce(
|
|
$data,
|
|
fn($output, $item) => [...$output, "$item $item"],
|
|
[]
|
|
);
|
|
|
|
|
|
$reduce=array_reduce(
|
|
$data,
|
|
function($output, $item) {
|
|
echo $item;
|
|
echo '<pre>';
|
|
print_r($output);
|
|
echo '</pre>';
|
|
echo '<br><br>';
|
|
return [...$output, "$item $item"];
|
|
},
|
|
[]
|
|
);
|
|
|
|
$filter=array_filter(
|
|
$data,
|
|
fn($item) => $item % 2 == 0
|
|
);
|
|
|
|
echo '<pre>';
|
|
print_r([...$data, 6, 7, 8]);
|
|
print_r([$data, 6, 7, 8]);
|
|
echo '</pre>';
|
|
|
|
|
|
echo '<br>';
|
|
echo '<pre>';
|
|
print_r($reduce);
|
|
echo '</pre>';
|
|
|
|
|
|
|
|
|