{"id":10020816,"date":"2025-06-06T22:44:11","date_gmt":"2025-06-06T14:44:11","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020816"},"modified":"2025-06-06T22:44:11","modified_gmt":"2025-06-06T14:44:11","slug":"%e5%88%86%e4%ba%ab%e4%b8%80%e4%ba%9b%e5%ae%9e%e7%94%a8%e7%9a%84php%e5%87%bd%e6%95%b0%e5%af%b9%e6%af%94js-ts%e5%ae%9e%e7%8e%b01","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020816","title":{"rendered":"\u5206\u4eab\u4e00\u4e9b\u5b9e\u7528\u7684PHP\u51fd\u6570(\u5bf9\u6bd4js\/ts\u5b9e\u73b0)(1)"},"content":{"rendered":"<h2>\u68c0\u67e5\u6570\u7ec4\u6240\u6709\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u7ed9\u5b9a\u6761\u4ef6<\/h2>\n<p>\u5982\u679c\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u8fd4\u56de <code>true<\/code>\uff0c\u5219\u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code>\u3002<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b:<\/p>\n<p>\u4f7f\u7528\u6570\u7ec4\u7684<code>filter<\/code>\u65b9\u6cd5\u5bf9\u6570\u7ec4\u6267\u884c\u7ed9\u5b9a\u7684\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528<code>count<\/code>\u65b9\u6cd5\u83b7\u53d6\u6267\u884c\u540e\u7684\u7ed3\u679c\uff0c\u518d\u548c<code>count<\/code>\u65b9\u6cd5\u83b7\u53d6\u672a\u6267\u884c<code>filter<\/code>\u65b9\u6cd5\u7684\u7ed3\u679c\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function all($arr,$fn){\n    return count(array_filter($arr,$fn)) === count($arr);\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">all([2, 3, 4, 5], function ($item) {\n  return $item &gt; 1;\n}); \/\/ true<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const all = (arr,fn) =&gt; arr.filter(fn).length === arr.length;<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const all = &lt;T,U&gt;(arr:T[], fn: (value: T, index: number, array: T[]) =&gt; U) =&gt; arr.filter(fn).length === arr.length;<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">all([2,3,4,5],n =&gt; n &gt; 1); \/\/ true<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u901a\u8fc7<code>count<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u800cjs\/ts\u5219\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7<code>length<\/code>\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/li>\n<li>php\u7684<code>filter<\/code>\u65b9\u6cd5\u540d\u53eb<code>array_filter<\/code>,\u800cjs\/ts\u5219\u662f\u53eb<code>filter<\/code>\u3002<\/li>\n<\/ol>\n<h2>\u68c0\u67e5\u6570\u7ec4\u6240\u6709\u5143\u7d20\u662f\u5426\u6709\u4e00\u9879\u6ee1\u8db3\u7ed9\u5b9a\u6761\u4ef6<\/h2>\n<p>\u5982\u679c\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6570\u7ec4\u7684\u81f3\u5c11\u4e00\u4e2a\u5143\u7d20\u8fd4\u56de <code>true<\/code>\uff0c\u5219\u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code>\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u548c\u524d\u9762\u7684<code>all<\/code>\u51fd\u6570\u5f88\u76f8\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u4e0d\u9700\u8981\u505a\u6bd4\u8f83\uff0c\u53ea\u9700\u8981\u5224\u65ad\u957f\u5ea6\u5927\u4e8e0\u5373\u53ef\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function any($arr,$fn){\n    return count(array_filter($arr,$fn)) &gt; 0;\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">any([2, 3, 4, 5], function ($item) {\n  return $item &lt; 3;\n}); \/\/ true<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const any = (arr,fn) =&gt; arr.filter(fn).length &gt; 0;<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const any = &lt;T,U&gt;(arr:T[], fn: (value: T, index: number, array: T[]) =&gt; U) =&gt; arr.filter(fn).length &gt; 0;<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">any([2,3,4,5],n =&gt; n &lt; 3); \/\/ true<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0eall\u7684\u603b\u7ed3\u4e00\u81f4\u3002<\/p>\n<h2>\u68c0\u67e5 2 \u4e2a\u6570\u5b57\u662f\u5426\u5927\u81f4\u76f8\u7b49<\/h2>\n<p>\u68c0\u67e5\u4e24\u4e2a\u6570\u5b57\u662f\u5426\u5927\u81f4\u76f8\u7b49\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u8be5\u51fd\u6570\u67093\u4e2a\u53c2\u6570\uff0c\u901a\u8fc7<code>abs<\/code>\u65b9\u6cd5\u8ba1\u7b97\u524d\u97622\u4e2a\u53c2\u6570\u7684\u7edd\u5bf9\u5dee\u503c\uff0c\u7136\u540e\u4e0e\u7b2c\u4e09\u4e2a\u53c2\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u5224\u65ad\u662f\u5426\u5c0f\u4e8e\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u9ed8\u8ba4\u503c\u662f0.001\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function apprEqual($a,$b,$c = 0.001){\n    return abs($a - $b) &lt; $c;\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">apprEqual(10.0, 10.00001); \/\/ true\napprEqual(10.0, 10.01); \/\/ false<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const apprEqual = (a,b,c = 0.001) =&gt; Math.abs(a - b) &lt; c;<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const apprEqual = (a: number,b:number,c = 0.001) =&gt; Math.abs(a - b) &lt; c;<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">apprEqual(10.0, 10.00001); \/\/ true\napprEqual(10.0, 10.01); \/\/ false<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u7684\u83b7\u53d6\u7edd\u5bf9\u503c\u7684\u65b9\u6cd5\u540d\u53eb<code>abs<\/code>,\u800cjs\/ts\u5219\u662f\u4ece<code>Math<\/code>\u5bf9\u8c61\u4e2d\u83b7\u53d6\uff0c\u5373<code>Math.abs<\/code>\u3002<\/li>\n<\/ol>\n<h2>\u5e73\u5747\u503c<\/h2>\n<p>\u8fd4\u56de\u4e24\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u7684\u5e73\u5747\u503c\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u6c42\u548c\uff0c\u7136\u540e\u9664\u4ee5\u957f\u5ea6<code>count<\/code>\u5373\u53ef\uff0c\u53c2\u6570\u53ef\u4ee5\u4f7f\u7528\u5c55\u5f00\u8fd0\u7b97\u7b26\u6765\u7ec4\u6210\u4e00\u4e2a\u6570\u7ec4,\u6ce8\u610f\u9700\u8981\u5199\u4e00\u4e2a\u4e09\u5143\u8868\u8fbe\u5f0f\uff0c\u5224\u65ad\u957f\u5ea6\u662f\u5426\u4e3a0\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function average(...$arr){\n    $len = count($arr);\n    return $len === 0 ? 0 : array_sum($arr) \/ $len;\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">average(1, 2, 3); \/\/ 2<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const average = (...arr) =&gt; {\n    const len = arr.length;\n    return len === 0 ? 0 : arr.reduce((r,i) =&gt; r += i,0) \/ len;\n}<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const average = (...arr: number[]):number =&gt; {\n    const len = arr.length;\n    return len === 0 ? 0 : arr.reduce((r,i) =&gt; r += i,0) \/ len;\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">average(1, 2, 3); \/\/ 2<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>array_sum<\/code>\u65b9\u6cd5\u6c42\u6570\u5b57\u6570\u7ec4\u7684\u548c\uff0cjs\/ts\u9700\u8981\u5faa\u73af\u81ea\u5df1\u8ba1\u7b97(\u6216\u8005\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a<code>sum<\/code>\u65b9\u6cd5)\u3002<\/li>\n<\/ol>\n<h2>\u6570\u503c\u9650\u5b9a<\/h2>\n<p>\u5c06\u6570\u503c\u9650\u5236\u5728\u8fb9\u754c\u503c <code>a<\/code> \u548c <code>b<\/code> \u6307\u5b9a\u7684\u5305\u542b\u8303\u56f4\u5185\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u5982\u679c\u8be5\u6570\u503c\u5728\u8303\u56f4\u5185\uff0c\u5219\u8fd4\u56de\u8be5\u6570\u503c\uff0c\u5426\u5219\uff0c\u4f7f\u7528\u6700\u5c0f\u503c\u65b9\u6cd5\u548c\u6700\u5927\u503c\u65b9\u6cd5\u8fd4\u56de\u8303\u56f4\u5185\u6700\u63a5\u8fd1\u7684\u6570\u5b57\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function clampNumber($num,$a,$b){\n    return max(min($num,max($a,$b)),min($a,$b));\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">clampNumber(2, 3, 5); \/\/ 3 \nclampNumber(1, -1, -5); \/\/ -1<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const clampNumber = (num,a,b) =&gt; {\n    const max = Math.max,min = Math.min;\n    return max(min(num,max(a,b)),min(a,b));\n}<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const clampNumber = (num:number,a:number,b:number) =&gt; {\n    const max = Math.max,min = Math.min;\n    return max(min(num,max(a,b)),min(a,b));\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">clampNumber(2, 3, 5); \/\/ 3 \nclampNumber(1, -1, -5); \/\/ -1<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u7684\u83b7\u53d6\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u7684\u65b9\u6cd5\u540d\u53eb<code>min<\/code>\u548c<code>max<\/code>,\u800cjs\/ts\u5219\u662f\u4ece<code>Math<\/code>\u5bf9\u8c61\u4e2d\u83b7\u53d6\uff0c\u5373<code>Math.min<\/code>\u548c<code>Math.max<\/code>\u3002<\/li>\n<\/ol>\n<h2>\u7ec4\u5408\u51fd\u6570<\/h2>\n<p>\u8fd4\u56de\u4e00\u4e2a\u5c06\u591a\u4e2a\u51fd\u6570\u7ec4\u5408\u6210\u5355\u4e2a\u53ef\u8c03\u7528\u51fd\u6570\u7684\u65b0\u51fd\u6570\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u4f7f\u7528 <code>array_reduce()<\/code> \u6267\u884c\u4ece\u53f3\u5230\u5de6\u7684\u51fd\u6570\u7ec4\u5408\u3002\u8be5\u51fd\u6570\u63a5\u65363\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u51fd\u6570\u5217\u8868\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u7528\u6765\u5408\u5e76\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u51fd\u6570\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u51fd\u6570\u7684\u521d\u59cb\u503c\uff0c\u5373<code>$function($x){ return $x; }<\/code>\uff0c\u9ed8\u8ba4\u662f\u4e00\u4e2a\u6052\u7b49\u51fd\u6570\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u8fd4\u56de\u5176\u8f93\u5165\u53c2\u6570\u672c\u8eab\u7684\u51fd\u6570\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function compose(...$fns){\n    return array_reduce(\n        $fns,\n        function ($carry,$fn){\n            \/\/ \u4f7f\u7528 use\u8bed\u53e5\u5bfc\u5165\u95ed\u5305\u4e2d\u7684\u53d8\u91cf$carry,$fn\n            return function($x) use ($carry,$fn){\n                return $carry($fn($x));\n            }\n        },\n        $function($x){\n            return $x;\n        }\n    )\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">$compose = compose(\n  \/\/ \u52a02\n  function ($x) {\n    return $x + 2;\n  },\n  \/\/ \u4e58\u4ee54\n  function ($x) {\n    return $x * 4;\n  }\n);\n$compose(3); \/\/ 20<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const compose = (...fns) =&gt; fns.reduce((f,h) =&gt; (...args) =&gt; f(h(...args)));<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">type ComposeFn&lt;T extends any&gt; = (...args: T[]) =&gt; T;\n\nconst compose = &lt;T,U extends ComposeFn&lt;T&gt;[]&gt;(...fns: U) =&gt;\n    fns.reduce(\n        (f, h) =&gt;\n            (...args: Parameters&lt;ComposeFn&lt;T&gt;&gt;) =&gt; f(h(...args))\n    );\n<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">const add5 = x =&gt; x + 5;\nconst multiply = (x, y) =&gt; x * y;\nconst multiplyAndAdd5 = compose(add5, multiply);\nconst res = multiplyAndAdd5(5, 2); \/\/ 15<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u9700\u8981\u4f7f\u7528<code>use<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u95ed\u5305\u4e2d\u7684\u53d8\u91cf\uff0c\u800cjs\/ts\u5219\u4e0d\u9700\u8981\u3002<\/li>\n<li>php\u4f7f\u7528<code>array_reduce<\/code>\u65b9\u6cd5\u6765\u904d\u5386\u51fd\u6570\u6570\u7ec4\uff0c\u5e76\u4e14\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u6052\u7b49\u51fd\u6570\u4f5c\u4e3a\u7b2c\u4e09\u4e2a\u53c2\u6570\u7684\u521d\u59cb\u503c\uff0c\u800cjs\/ts\u76f4\u63a5\u8c03\u7528\u6570\u7ec4\u7684<code>reduce<\/code>\u65b9\u6cd5\u6765\u904d\u5386\u51fd\u6570\u6570\u7ec4\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u63d0\u4f9b\u7b2c\u4e09\u4e2a\u4f5c\u4e3a\u521d\u59cb\u503c\u7684\u53c2\u6570\u3002<\/li>\n<\/ol>\n<h2>\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5143\u97f3\u5b57\u6bcd\u7684\u6570\u91cf<\/h2>\n<p>\u8fd4\u56de\u6240\u63d0\u4f9b\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u97f3\u5b57\u6bcd\u6570\u91cf\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5143\u97f3\u5b57\u6bcd\uff0c\u5143\u97f3\u5b57\u6bcd\u5305\u542ba\u3001e\u3001i\u3001o\u3001u\u7b49\u5b57\u6bcd\u3002\u8c03\u7528php\u7684<code>preg_match_all<\/code>\u65b9\u6cd5\u6765\u5339\u914d\uff0c\u7136\u540e\u4f7f\u7528<code>count<\/code>\u65b9\u6cd5\u8ba1\u7b97\u8fd4\u56de\u503c\u7684\u957f\u5ea6\uff0c\u5373\u4e3a\u5143\u97f3\u5b57\u6bcd\u7684\u6570\u91cf\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function countVowels($str){\n    preg_match_all('\/[aeiou]\/i',$str,$matches);\n    \/\/ $matches\u4e3a\u5339\u914d\u7ed3\u679c\n    return count($matches[0]);\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">countVowels('sampleInput'); \/\/ 4<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const countVowels = (str) =&gt; [...str.matchAll(\/[aeiou]\/ig)].length;<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const countVowels = (str: string) =&gt; [...str.matchAll(\/[aeiou]\/ig)].length;<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">countVowels('sampleInput'); \/\/ 4<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u4f7f\u7528<code>preg_match_all<\/code>\u65b9\u6cd5\u6765\u5339\u914d\u5b57\u7b26\u4e32\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5b57\u7b26\uff0c\u800cjs\/ts\u5219\u4f7f\u7528<code>String.matchAll<\/code>\u65b9\u6cd5\u3002<\/li>\n<li>php \u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u548cjs\/ts\u6709\u6240\u533a\u522b\uff0c\u9700\u8981\u628a\u6b63\u5219\u8868\u8fbe\u5f0f\u5f53\u4f5c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/li>\n<li>js\/ts\u8c03\u7528<code>matchAll<\/code>\u65b9\u6cd5\u65f6\u9700\u8981\u63d0\u4f9b\u4e00\u4e2ag\u4fee\u9970\u7b26\uff0c\u8868\u793a\u7528\u6765\u5339\u914d\u5168\u5c40,php\u5219\u4e0d\u9700\u8981\u3002<\/li>\n<li>js\/ts\u8c03\u7528<code>matchAll<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u9700\u8981\u4f7f\u7528\u5c55\u5f00\u8fd0\u7b97\u7b26\u8f6c\u6362\u6210\u6570\u7ec4\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u957f\u5ea6\u3002<\/li>\n<li>php\u4f7f\u7528<code>count<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u800cjs\/ts\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7<code>length<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u3002<\/li>\n<\/ol>\n<h2>\u67ef\u91cc\u5316\u51fd\u6570<\/h2>\n<p>\u5bf9\u51fd\u6570\u8fdb\u884c\u67ef\u91cc\u5316\uff0c\u4ee5\u4fbf\u5728\u591a\u6b21\u8c03\u7528\u4e2d\u83b7\u53d6\u53c2\u6570\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><code>curry<\/code>\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\uff0c\u8fd4\u56de\u6267\u884c\u7684\u7d2f\u79ef\u5668<code>$acc<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>&amp;<\/code>\u64cd\u4f5c\u7b26\u5f15\u7528\u7d2f\u79ef\u5668\uff0c\u4f7f\u7528<code>use<\/code>\u8bed\u53e5\u5bfc\u5165\u51fd\u6570\u548c\u7d2f\u79ef\u5668\u3002<\/li>\n<li>\u4f7f\u7528<code>array_merge<\/code>\u65b9\u6cd5\u5408\u5e76\u6240\u6709\u53c2\u6570\u3002<\/li>\n<li>\u4f7f\u7528php\u53cd\u5c04<code>ReflectionFunction<\/code>\u83b7\u53d6\u51fd\u6570\u6240\u9700\u53c2\u6570\u6570\u91cf\uff0c\u7136\u540e\u8c03\u7528<code>getNumberOfRequiredParameters<\/code>\u65b9\u6cd5\u83b7\u53d6\u5fc5\u9700\u53c2\u6570\u7684\u6570\u91cf\u3002<\/li>\n<li>\u5982\u679c\u5fc5\u9700\u53c2\u6570\u6570\u91cf\u5c0f\u4e8e\u7b49\u4e8e\u5916\u90e8\u53c2\u6570\u6570\u91cf\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u51fd\u6570\u6267\u884c\u53c2\u6570\u7ed3\u679c\uff0c\u5426\u5219\u9012\u5f52\u7684\u6267\u884c\u7d2f\u79ef\u5668\u3002<\/li>\n<\/ol>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function curry($fn){\n    $acc = function ($arg) use ($fn,&amp;$acc){\n        return function (...$args) use($fn,$arg,$acc){\n            \/\/ \u5408\u5e76\u53c2\u6570\n            $arg = array_merge($args);\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u53cd\u5c04\n            $ref = new ReflectionFunction($fn);\n            \/\/ \u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u5fc5\u9700\u53c2\u6570\n            $totalArgs = $ref -&gt; getNumberOfRequiredParameters();\n            \n            if($totalArgs &lt;= count($arg)){\n                return $fn(...$arg);\n            }\n            \n            return $acc($arg);\n        }\n    }\n    \n    return $acc([]);\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">$curriedAdd = curry(function ($a, $b) { return $a + $b; }); \n$add10 = $curriedAdd(10); \nvar_dump($add10(15)); \/\/ 25<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const curry = fn =&gt; (...args) =&gt; args.length &gt;= fn.length ? fn(...args) : (...nArgs) =&gt; curry(fn)(...args,...nArgs);<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const curry =\n  &lt;T, R, F extends (...args: T[]) =&gt; R&gt;(fn: F) =&gt;\n  (...args: T[]) =&gt;\n    args.length &gt;= fn.length\n      ? fn(...args)\n      : (...nArgs: T[]) =&gt; curry(fn as (...args: unknown[]) =&gt; unknown)(...args, ...nArgs);<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">const curriedAdd = curry((a,b) =&gt; a + b); \nconst add10 = curriedAdd(10); \nconst res = add10(15); \/\/ 25<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u4e2d\u4f7f\u7528\u4e86\u53cd\u5c04\u76f8\u5173\u65b9\u6cd5\u6765\u83b7\u53d6\u53c2\u6570,\u800cjs\/ts\u5219\u4e0d\u9700\u8981\u3002<\/li>\n<li>php\u4f7f\u7528<code>array_merge<\/code>\u65b9\u6cd5\u6765\u5408\u5e76\u53c2\u6570\uff0c\u800cjs\/ts\u5219\u4e0d\u9700\u8981\u3002<\/li>\n<li>\u4e24\u8005\u7684\u5224\u65ad\u903b\u8f91\u4e5f\u6709\u5dee\u5f02\u3002<\/li>\n<\/ol>\n<p>ps: \u4e24\u8005\u7684\u5b9e\u73b0\u90fd\u91c7\u7528\u4e86\u9012\u5f52\u7684\u65b9\u5f0f\uff0c\u8fd9\u70b9\u9700\u8981\u6ce8\u610f\u3002<\/p>\n<h2>\u53d6\u6d88\u5b57\u7b26\u4e32\u9996\u5b57\u6bcd\u5927\u5199<\/h2>\n<p>\u53d6\u6d88\u5b57\u7b26\u4e32\u9996\u5b57\u6bcd\u7684\u5927\u5199\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u8c03\u7528<code>lcfirst<\/code>\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u5927\u5199\u9996\u5b57\u6bcd\u8f6c\u6362\u6210\u5c0f\u5199\uff0c\u6839\u636e\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>$upperRest<\/code>\u6765\u51b3\u5b9a\u662f\u5426\u5c06\u5176\u4f59\u5b57\u6bcd\u8f6c\u6362\u6210\u5927\u5199\uff0c\u4f7f\u7528<code>strtoupper<\/code>\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function decapitalize($str,$upperRest = false){\n    return lcfirst($upperRest ? strtoupper($str) : $str);\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">decapitalize('FooBar'); \/\/ 'fooBar'<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const decapitalize = (str, upperRest = false) =&gt; str.slice(0,1).toLowerCase() + `${upperRest ? str.slice(1).toUpperCase() : str.slice(1)}`;<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const decapitalize = (str: string, upperRest = false) =&gt; str.slice(0,1).toLowerCase() + `${upperRest ? str.slice(1).toUpperCase() : str.slice(1)}`;<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">decapitalize('FooBar'); \/\/ 'fooBar'<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u6709\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u5c06\u9996\u5b57\u6bcd\u8f6c\u6362\u6210\u5c0f\u5199\uff0c\u800cjs\/ts\u5219\u9700\u8981\u901a\u8fc7<code>slice<\/code>\uff08\u6216\u5176\u5b83\u5b57\u7b26\u4e32\u7684\u622a\u53d6\u65b9\u6cd5\uff0c\u4f8b\u5982:<code>substr<\/code>\uff09\u65b9\u6cd5\u6765\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u62fc\u63a5\u5728\u4e00\u8d77\u3002<\/li>\n<li>php\u8f6c\u6362\u6210\u5927\u5199\u7684\u65b9\u6cd5\u662f<code>strtoupper<\/code>,\u800cjs\/ts\u5219\u662f\u901a\u8fc7\u8c03\u7528<code>String.toUpperCase<\/code>\u65b9\u6cd5\u6765\u8f6c\u6362\u6210\u5927\u5199\u3002<\/li>\n<\/ol>\n<h2>\u6df1\u5ea6\u5c55\u5f00\u6570\u7ec4<\/h2>\n<p>\u6df1\u5ea6\u5c55\u5e73\u6570\u7ec4,\u76f4\u63a5\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6210\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<h3>\u5b9e\u73b0\u601d\u8def<\/h3>\n<p>\u5b9e\u73b0\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>\u4f7f\u7528<code>foreach<\/code>\u5faa\u73af\u6570\u7ec4,\u5982\u679c\u6570\u7ec4\u9879\u662f\u6570\u7ec4\uff08\u4f7f\u7528<code>is_array<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\uff09\uff0c\u5219\u4f7f\u7528\u9012\u5f52\u52a0<code>array_push<\/code>\u548c\u7a7a\u6570\u7ec4\u4ee5\u53ca\u5c55\u5f00\u8fd0\u7b97\u7b26\u6765\u5408\u5e76\u6570\u7ec4\uff0c\u5426\u5219\u76f4\u63a5\u4f7f\u7528<code>[]<\/code>\u8bed\u6cd5\u6765\u6dfb\u52a0\u6570\u7ec4\u9879(\u76f8\u5f53\u4e8e<code>array_push<\/code>\u65b9\u6cd5)\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770bphp\u548cjs\u4ee5\u53cats\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<h4>php\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"php\">function deepFlatten($items){\n    $res = [];\n    foreach($item as $items){\n        if(is_array($item)){\n            array_push($res,...deepFlatten($item));\n        }else{\n            $res[] = $item;\n        }\n    }\n    return $res;\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"php\">deepFlatten([1, [2], [[3], 4], 5]); \/\/ [1, 2, 3, 4, 5]<\/code><\/pre>\n<h4>js\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"js\">const deepFlatten = (items) =&gt; {\n  let res = [];\n  for(const item of items){\n    if(Array.isArray(item) &amp;&amp; item.length &gt; 0){\n      res.push(...deepFlatten(item))\n    }else{\n      res.push(item)\n    }\n  }\n  return res;\n}<\/code><\/pre>\n<h4>ts\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<pre><code class=\"ts\">const deepFlatten = &lt;T&gt;(items: T[]) =&gt; {\n  let res: T[] = [];\n  for (const item of items) {\n    if (Array.isArray(item) &amp;&amp; item.length &gt; 0) {\n      res.push(...deepFlatten(item));\n    } else {\n      res.push(item);\n    }\n  }\n  return res;\n};<\/code><\/pre>\n<h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"js\">deepFlatten([1, [2], [[3], 4], 5]); \/\/ [1, 2, 3, 4, 5]<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u4e0ejs\/ts\u5b9e\u73b0\u4e0d\u540c\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>php\u4f7f\u7528<code>array_push<\/code>\u65b9\u6cd5\u6765\u5f80\u6570\u7ec4\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\uff0c\u800cjs\/ts\u5219\u662f\u76f4\u63a5\u4f7f\u7528<code>push<\/code>\u65b9\u6cd5\u3002<\/li>\n<li>php\u53ef\u4ee5\u4f7f\u7528<code>[]<\/code>\u8bed\u6cd5\u6765\u4ee3\u66ff<code>array_push<\/code>\u65b9\u6cd5\uff0cjs\/ts\u6ca1\u6709\u76f8\u5173\u8bed\u6cd5\u3002<\/li>\n<li>php\u4f7f\u7528<code>is_array<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u662f\u5426\u662f\u6570\u7ec4\uff0c\u800cjs\/ts\u5219\u4f7f\u7528<code>Array.isArray<\/code>\u65b9\u6cd5\u3002<\/li>\n<li>php\u4f7f\u7528<code>foreach<\/code>\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0cjs\/ts\u6ca1\u6709<code>foreach<\/code>\u5faa\u73af\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u4ee3\u66ff\u3002<\/li>\n<\/ol>\n<h2>\u6700\u540e<\/h2>\n<p>\u611f\u8c22\u5927\u5bb6\u9605\u8bfb\u672c\u6587\uff0c\u5982\u679c\u6709\u6536\u83b7\u548c\u5e2e\u52a9\uff0c\u5e0c\u671b\u4e0d\u541d\u556c\u70b9\u8d5e\u6536\u85cf\uff0c\u4ee5\u4e0a\u4ee3\u7801\u6bb5\u6765\u81ea\u7f51\u7edc\uff0c\u5e76\u603b\u7ed3\u5230\u6211\u7684<a href=\"https:\/\/link.segmentfault.com\/?enc=XsTmVAaVbP0SX1DsexFDJQ%3D%3D.5cytauZUsGC0YE3T1OIya2I2ZzJizyPnltn1A7R6t4SXLeGE4cTOvELEUmi8dTmj0fpXa%2FOBI2l0lAmwGMGbmQ%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u4ee3\u7801\u6bb5\u7f51\u7ad9<\/a>\u4e2d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046612648\">https:\/\/segmentfault.com\/a\/1190000046612648<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u68c0\u67e5\u6570\u7ec4\u6240\u6709\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u7ed9\u5b9a\u6761\u4ef6 \u5982\u679c\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u8fd4\u56de true\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de f [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020816","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020816"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020816\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}