{"id":47156,"date":"2024-12-02T12:33:56","date_gmt":"2024-12-02T04:33:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47156\/"},"modified":"2024-12-02T12:33:56","modified_gmt":"2024-12-02T04:33:56","slug":"php-%e5%87%bd%e6%95%b0%e5%9c%a8%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90%e4%b8%ad%e7%9a%84%e5%ae%9e%e8%b7%b5%e7%bb%8f%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47156\/","title":{"rendered":"PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u5e7f\u6cdb\u5e94\u7528\uff0c\u6db5\u76d6\u6570\u636e\u6e05\u7406\u3001\u8f6c\u6362\u3001\u7edf\u8ba1\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\uff1a\u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362\uff1aarray_filter() \u79fb\u9664\u7279\u5b9a\u5143\u7d20\uff0cstr_replace() \u66ff\u6362\u5b50\u5b57\u7b26\u4e32\uff0cexplode() \u62c6\u5206\u5b57\u7b26\u4e32\u3002\u7edf\u8ba1\u5206\u6790\uff1aarray_sum() \u8ba1\u7b97\u603b\u548c\uff0carray_count_values() \u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0cvar_dump() \u67e5\u770b\u53d8\u91cf\u503c\u3002\u673a\u5668\u5b66\u4e60\uff1aarray_rand() \u968f\u673a\u9009\u62e9\u5143\u7d20\uff0c\u7ed3\u5408\u673a\u5668\u5b66\u4e60\u5e93\u53ef\u6784\u5efa\u9884\u6d4b\u6a21\u578b\uff08\u5982\u5ba2\u6237\u6d41\u5931\u9884\u6d4b\uff09\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729901379671c334316db2.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u811a\u672c\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u548c\u6570\u636e\u5904\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u53ef\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4ece\u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362\u5230\u7edf\u8ba1\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u89c1\u7684 PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362<\/strong><\/p>\n<p><strong>array_filter()\uff1a<\/strong>\u79fb\u9664\u6570\u7ec4\u4e2d\u6ee1\u8db3\u6307\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<pre>$data = ['name' =&gt; null, 'age' =&gt; 30, 'city' =&gt; 'Tokyo'];\n$filtered_data = array_filter($data);\n\/\/ \u8f93\u51fa\uff1a['age' =&gt; 30, 'city' =&gt; 'Tokyo']<\/pre>\n<p><strong>str_replace()\uff1a<\/strong>\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>$text = \"My name is John Doe. My age is 30.\";\n$replaced_text = str_replace('John Doe', 'Jane Doe', $text);\n\/\/ \u8f93\u51fa\uff1a\"My name is Jane Doe. My age is 30.\"<\/pre>\n<p><strong>explode()\uff1a<\/strong>\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u6570\u7ec4\uff0c\u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u3002<\/p>\n<pre>$csv_data = \"John,30,Tokyo\";\n$exploded_data = explode(',', $csv_data);\n\/\/ \u8f93\u51fa\uff1a['John', '30', 'Tokyo']<\/pre>\n<p><strong>\u7edf\u8ba1\u5206\u6790<\/strong><\/p>\n<p><strong>array_sum()\uff1a<\/strong>\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002<\/p>\n<pre>$data = [10, 20, 30];\n$sum = array_sum($data);\n\/\/ \u8f93\u51fa\uff1a60<\/pre>\n<p><strong>array_count_values()\uff1a<\/strong>\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<pre>$data = ['a', 'b', 'a', 'c', 'b'];\n$counts = array_count_values($data);\n\/\/ \u8f93\u51fa\uff1a['a' =&gt; 2, 'b' =&gt; 2, 'c' =&gt; 1]<\/pre>\n<p><strong>var_dump()\uff1a<\/strong>\u67e5\u770b\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u503c\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u76ee\u7684\u5f88\u6709\u7528\u3002<\/p>\n<pre>$data = ['name' =&gt; 'John Doe', 'age' =&gt; 30];\nvar_dump($data);\n\/\/ \u8f93\u51fa\uff1aarray(2) { [\"name\"]=&gt; string(7) \"John Doe\" [\"age\"]=&gt; int(30) }<\/pre>\n<p><strong>\u673a\u5668\u5b66\u4e60<\/strong><\/p>\n<p><strong>array_rand()\uff1a<\/strong>\u4ece\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u5bf9\u4e8e\u62bd\u6837\u548c\u521b\u5efa\u8bad\u7ec3\u6570\u636e\u96c6\u5f88\u6709\u7528\u3002<\/p>\n<pre>$data = [1, 2, 3, 4, 5];\n$random_index = array_rand($data);\n$random_element = $data[$random_index];\n\/\/ \u8f93\u51fa\uff1a\u4f8b\u5982\uff0c3<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5ba2\u6237\u6d41\u5931\u9884\u6d4b<\/strong><\/p>\n<p>\u6211\u4eec\u4f7f\u7528 PHP \u6570\u636e\u5206\u6790\u51fd\u6570\u6784\u5efa\u4e00\u4e2a\u5ba2\u6237\u6d41\u5931\u9884\u6d4b\u6a21\u578b\u3002<\/p>\n<pre>$data = [\n    \/\/ \u7279\u5f81\uff1a\u5e74\u9f84\u3001\u6536\u5165\u3001\u6027\u522b\u3001\u662f\u5426\u5fe0\u8bda\u4f1a\u5458\n    ['age' =&gt; 30, 'income' =&gt; 50000, 'gender' =&gt; 'male', 'loyal' =&gt; true],\n    \/\/ ... \u5176\u4ed6\u5ba2\u6237\u6570\u636e\n];\n\n\/\/ \u8bad\u7ec3\u6570\u636e\n$X_train = array_map(function($row) {\n    \/\/ \u79fb\u9664\u76ee\u6807\u53d8\u91cf\uff08\u662f\u5426\u6d41\u5931\uff09\n    unset($row['loyal']);\n    return $row;\n}, $data);\n\n$y_train = array_column($data, 'loyal');\n\n\/\/ \u8bad\u7ec3\u903b\u8f91\u56de\u5f52\u6a21\u578b\n$model = new \\LogisticRegression();\n$model-&gt;train($X_train, $y_train);\n\n\/\/ \u6d4b\u8bd5\u6570\u636e\n$X_test = array_map(function($row) {\n    \/\/ \u79fb\u9664\u76ee\u6807\u53d8\u91cf\n    unset($row['loyal']);\n    return $row;\n}, $data);\n\n\/\/ \u9884\u6d4b\u6982\u7387\n$y_pred_proba = $model-&gt;predict_proba($X_test);\n\n\/\/ \u8bc4\u4f30\u6a21\u578b\n$accuracy = metrics\\accuracy_score($y_test, $y_pred_proba);<\/pre>\n<p>\u901a\u8fc7\u7ed3\u5408 PHP \u7684\u6570\u636e\u5206\u6790\u51fd\u6570\u548c\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u6211\u4eec\u80fd\u591f\u6784\u5efa\u4e00\u4e2a\u9884\u6d4b\u5ba2\u6237\u6d41\u5931\u7684\u6a21\u578b\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u5e7f\u6cdb\u5e94\u7528\uff0c\u6db5\u76d6\u6570\u636e\u6e05\u7406\u3001\u8f6c\u6362\u3001\u7edf\u8ba1\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\uff1a\u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362\uff1aarray_filter() \u79fb\u9664\u7279\u5b9a\u5143\u7d20\uff0cstr_replace() \u66ff\u6362\u5b50\u5b57\u7b26\u4e32\uff0cexplode() \u62c6\u5206\u5b57\u7b26\u4e32\u3002\u7edf\u8ba1\u5206\u6790\uff1aarray_sum() \u8ba1\u7b97\u603b\u548c\uff0carray_count_values() \u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0cvar_dump() \u67e5\u770b\u53d8\u91cf\u503c\u3002\u673a\u5668\u5b66\u4e60\uff1aarray_rand() \u968f\u673a\u9009\u62e9\u5143\u7d20\uff0c\u7ed3\u5408\u673a\u5668\u5b66\u4e60\u5e93\u53ef\u6784\u5efa\u9884\u6d4b\u6a21\u578b\uff08\u5982\u5ba2\u6237\u6d41\u5931\u9884\u6d4b\uff09\u3002 PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u8df5\u7ecf\u9a8c \u7b80\u4ecb PHP \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u811a\u672c\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u548c\u6570\u636e\u5904\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u53ef\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4ece\u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362\u5230\u7edf\u8ba1\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u89c1\u7684 PHP \u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u6218\u6848\u4f8b\u3002 \u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362 array_filter()\uff1a\u79fb\u9664\u6570\u7ec4\u4e2d\u6ee1\u8db3\u6307\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002 $data = [&#8216;name&#8217; =&gt; null, &#8216;age&#8217; =&gt; 30, &#8216;city&#8217; =&gt; &#8216;Tokyo&#8217;]; $filtered_data = array_filter($data); \/\/ \u8f93\u51fa\uff1a[&#8216;age&#8217; =&gt; 30, &#8216;city&#8217; =&gt; &#8216;Tokyo&#8217;] str_replace()\uff1a\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002 $text = &#8220;My name is John Doe. My age [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47156","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47156"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47156\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}