{"id":2979,"date":"2025-03-19T00:01:43","date_gmt":"2025-03-18T16:01:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2979"},"modified":"2025-03-19T00:01:43","modified_gmt":"2025-03-18T16:01:43","slug":"php-%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-%e6%95%b0%e7%bb%84\/","title":{"rendered":"PHP \u6570\u7ec4"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\uff0c\u6570\u7ec4\u662f\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u503c\u3002\u6570\u7ec4\u5728 PHP \u4e2d\u6709\u4e24\u79cd\u4e3b\u8981\u7c7b\u578b\uff1a<strong>\u7d22\u5f15\u6570\u7ec4<\/strong>\u548c<strong>\u5173\u8054\u6570\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u7d22\u5f15\u6570\u7ec4\uff08Indexed Arrays\uff09<\/strong><\/h2>\n\n\n\n<p>\u7d22\u5f15\u6570\u7ec4\u662f\u4ee5\u6570\u5b57\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u6570\u7ec4\u7684\u7d22\u5f15\u9ed8\u8ba4\u4ece 0 \u5f00\u59cb\uff0c\u9664\u975e\u4f60\u660e\u786e\u6307\u5b9a\u7d22\u5f15\u503c\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$fruits = array(\"Apple\", \"Banana\", \"Orange\");\necho $fruits&#91;0]; \/\/ \u8f93\u51fa Apple<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u5173\u8054\u6570\u7ec4\uff08Associative Arrays\uff09<\/strong><\/h2>\n\n\n\n<p>\u5173\u8054\u6570\u7ec4\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u952e\uff08\u901a\u5e38\u662f\u5b57\u7b26\u4e32\uff09\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u6570\u5b57\u7d22\u5f15\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$person = array(\"name\" =&gt; \"John\", \"age\" =&gt; 30, \"city\" =&gt; \"New York\");\necho $person&#91;\"name\"]; \/\/ \u8f93\u51fa John<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u591a\u7ef4\u6570\u7ec4\uff08Multidimensional Arrays\uff09<\/strong><\/h2>\n\n\n\n<p>PHP \u6570\u7ec4\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6570\u7ec4\uff0c\u5f62\u6210\u591a\u7ef4\u6570\u7ec4\u3002\u8fd9\u7c7b\u6570\u7ec4\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$students = array(\n    array(\"name\" =&gt; \"John\", \"age\" =&gt; 20),\n    array(\"name\" =&gt; \"Jane\", \"age\" =&gt; 22)\n);\necho $students&#91;0]&#91;\"name\"]; \/\/ \u8f93\u51fa John<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u5e38\u7528\u6570\u7ec4\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u7ec4\u51fd\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>count()<\/code>\uff1a\u8fd4\u56de\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/li>\n\n\n\n<li><code>array_push()<\/code>\uff1a\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/li>\n\n\n\n<li><code>array_pop()<\/code>\uff1a\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>array_merge()<\/code>\uff1a\u5408\u5e76\u4e24\u4e2a\u6216\u66f4\u591a\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><code>in_array()<\/code>\uff1a\u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = array(\"red\", \"green\", \"blue\");\necho count($colors); \/\/ \u8f93\u51fa 3\narray_push($colors, \"yellow\"); \/\/ \u6dfb\u52a0\u9ec4\u8272\u5230\u6570\u7ec4\necho in_array(\"blue\", $colors) ? \"Yes\" : \"No\"; \/\/ \u8f93\u51fa Yes<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u6570\u7ec4\u904d\u5386<\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u904d\u5386\u6570\u7ec4\uff0c\u6700\u5e38\u89c1\u7684\u662f\u4f7f\u7528 <code>foreach<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$fruits = array(\"Apple\", \"Banana\", \"Orange\");\nforeach ($fruits as $fruit) {\n    echo $fruit . \"\\n\";  \/\/ \u8f93\u51fa\u6bcf\u4e2a\u6c34\u679c\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>\u6570\u7ec4\u6392\u5e8f<\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6bd4\u5982 <code>sort()<\/code>\u3001<code>asort()<\/code> \u548c <code>ksort()<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sort()<\/code>\uff1a\u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><code>asort()<\/code>\uff1a\u6839\u636e\u6570\u7ec4\u7684\u503c\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u4fdd\u6301\u952e\u503c\u5173\u7cfb\u3002<\/li>\n\n\n\n<li><code>ksort()<\/code>\uff1a\u6839\u636e\u6570\u7ec4\u7684\u952e\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$numbers = array(3, 1, 4, 1, 5);\nsort($numbers); \/\/ \u5347\u5e8f\u6392\u5e8f\nprint_r($numbers); \/\/ \u8f93\u51fa &#91;1, 1, 3, 4, 5]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u5f15\u7528\u4f20\u9012<\/strong><\/h2>\n\n\n\n<p>\u5728 PHP \u4e2d\uff0c\u6570\u7ec4\u9ed8\u8ba4\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u6570\u7ec4\uff0c\u6539\u53d8\u539f\u6570\u7ec4\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function addElement(&amp;$array) {\n    $array&#91;] = \"New Element\";\n}\n\n$colors = array(\"red\", \"green\", \"blue\");\naddElement($colors);\nprint_r($colors); \/\/ \u8f93\u51fa &#91;'red', 'green', 'blue', 'New Element']<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong>JSON \u4e0e\u6570\u7ec4<\/strong><\/h2>\n\n\n\n<p>PHP \u6570\u7ec4\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e JSON \u6570\u636e\u8fdb\u884c\u8f6c\u6362\uff0c\u4f7f\u7528 <code>json_encode()<\/code> \u548c <code>json_decode()<\/code> \u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\"name\" =&gt; \"John\", \"age\" =&gt; 30);\n$json = json_encode($array);  \/\/ \u6570\u7ec4\u8f6c JSON\necho $json;  \/\/ \u8f93\u51fa {\"name\":\"John\",\"age\":30}\n\n$json_string = '{\"name\":\"John\",\"age\":30}';\n$array = json_decode($json_string, true);  \/\/ JSON \u8f6c\u6570\u7ec4\nprint_r($array);  \/\/ \u8f93\u51fa Array ( &#91;name] =&gt; John &#91;age] =&gt; 30 )<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/book.array.php\">PHP \u5b98\u65b9\u6570\u7ec4\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/ref.array.php\">PHP \u6570\u7ec4\u51fd\u6570\u5927\u5168<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\uff0c\u6570\u7ec4\u662f\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u503c\u3002\u6570\u7ec4\u5728 PHP \u4e2d\u6709\u4e24\u79cd\u4e3b\u8981\u7c7b\u578b\uff1a\u7d22\u5f15\u6570\u7ec4\u548c\u5173\u8054 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2980,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-2979","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2979"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2979\/revisions"}],"predecessor-version":[{"id":2981,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2979\/revisions\/2981"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2980"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}