{"id":119,"date":"2020-01-18T19:52:52","date_gmt":"2020-01-18T11:52:52","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=119"},"modified":"2020-01-18T19:52:52","modified_gmt":"2020-01-18T11:52:52","slug":"php%e5%ae%9e%e7%8e%b0%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%ef%bc%88%e6%88%96%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84%ef%bc%89%e8%bd%ac%e6%8d%a2%e6%88%90%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e7%9a%84","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2020\/01\/18\/php%e5%ae%9e%e7%8e%b0%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%ef%bc%88%e6%88%96%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84%ef%bc%89%e8%bd%ac%e6%8d%a2%e6%88%90%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e7%9a%84\/","title":{"rendered":"PHP\u5b9e\u73b0\u4e8c\u7ef4\u6570\u7ec4\uff08\u6216\u591a\u7ef4\u6570\u7ec4\uff09\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u7684\u5e38\u89c1\u65b9\u6cd5\u603b\u7ed3"},"content":{"rendered":"\n<p>\u672c\u6587\u5b9e\u4f8b\u603b\u7ed3\u4e86PHP\u5b9e\u73b0\u4e8c\u7ef4\u6570\u7ec4\uff08\u6216\u591a\u7ef4\u6570\u7ec4\uff09\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u5206\u4eab\u7ed9\u5927\u5bb6\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e0b\u9762\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = array(\n  '0' => array('id' => 100, 'username' => 'a1'),\n  '1' => array('id' => 101, 'username' => 'a2'),\n  '2' => array('id' => 102, 'username' => 'a3'),\n  '3' => array('id' => 103, 'username' => 'a4'),\n  '4' => array('id' => 104, 'username' => 'a5'),\n);<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u8981\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u6709\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p>\u4e00\u79cd\u662f\u5c06\u6307\u5b9a\u5217\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u8fd9\u5728\u53e6\u4e00\u7bc7\u6587\u7ae0\u6709\u603b\u7ed3\uff1a<a target=\"_blank\" href=\"https:\/\/www.jb51.net\/article\/175704.htm\" rel=\"noreferrer noopener\">PHP\u63d0\u53d6\u591a\u7ef4\u6570\u7ec4\u6307\u5b9a\u4e00\u5217\u7684\u65b9\u6cd5\u603b\u7ed3<\/a>\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u91cd\u70b9\u8bb2\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u5c31\u662f\u628a\u6240\u6709\u7684\u503c\u90fd\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u800c\u4e14\u952e\u503c\u76f8\u540c\u4e0d\u4f1a\u88ab\u8986\u76d6\uff0c\u8f6c\u6362\u540e\u7684\u4e00\u7ef4\u6570\u7ec4\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$result = array(100, 'a1'\uff0c 101, 'a2', 102, 'a3', 103, 'a4', 104, 'a5');<\/code><\/pre>\n\n\n\n<p>\u4e3b\u8981\u6709\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>1 array_reduce\u51fd\u6570\u6cd5<\/strong><\/p>\n\n\n\n<p>\u7528array_reduce()\u51fd\u6570\u662f\u8f83\u4e3a\u5feb\u6377\u7684\u65b9\u6cd5\uff1a<a href=\"https:\/\/www.jb51.net\/article\/175710.htm#\">?<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$result = array_reduce($user, function ($result, $value) {\n  return array_merge($result, array_values($value));\n}, array())<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3aarray_merge\u51fd\u6570\u4f1a\u628a\u76f8\u540c\u5b57\u7b26\u4e32\u952e\u540d\u7684\u6570\u7ec4\u8986\u76d6\u5408\u5e76\uff0c\u6240\u4ee5\u5fc5\u987b\u5148\u7528array_value\u53d6\u51fa\u503c\u540e\u518d\u5408\u5e76\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u7b2c\u4e8c\u7ef4\u662f\u6570\u5b57\u952e\u540d\uff0c\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = array(\n  'a' => array(100, 'a1'),\n  'b' => array(101, 'a2'),\n  'c' => array(102, 'a3'),\n  'd' => array(103, 'a4'),\n  'e' => array(104, 'a5'),\n);<\/code><\/pre>\n\n\n\n<p>\u90a3\u4e48\u76f4\u63a5\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$result = array_reduce($user, 'array_merge', array())<\/code><\/pre>\n\n\n\n<p><strong>2 array_walk_recursive\u51fd\u6570\u6cd5<\/strong><\/p>\n\n\n\n<p>\u7528array_walk_recursive()\u51fd\u6570\u5c31\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u628a\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$result = [];\narray_walk_recursive($user, function($value) use (&amp;$result) {\n  array_push($result, $value);\n});<\/code><\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u591a\u7ef4\u6570\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user4 = array(\n  'a' => array(100, 'a1'),\n  'b' => array(101, 'a2'),\n  'c' => array(\n    'd' => array(102, 'a3'),\n    'e' => array(103, 'a4'),\n  ),\n);<\/code><\/pre>\n\n\n\n<p>\u7528\u8fd9\u4e2a\u65b9\u6cd5\u540e\u5c31\u53d8\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$result = array(100, 'a1'\uff0c 101, 'a2', 102, 'a3', 103, 'a4');<\/code><\/pre>\n\n\n\n<p><strong>3 array_map\u51fd\u6570\u6cd5<\/strong><\/p>\n\n\n\n<p>\u7528array_map\u548carray_reduce\u51fd\u6570\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$result = [];\narray_map(function ($value) use (&amp;$result) {\n  $result = array_merge($result, array_values($value));\n}, $user);<\/code><\/pre>\n\n\n\n<p>\u53ea\u662f\u9700\u8981\u591a\u58f0\u660e\u4e00\u4e2a\u7a7a\u7684$result\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u7528array_walk\u7684\u65b9\u6cd5\uff0c\u548cforeach\u5faa\u73af\u7684\u65b9\u6cd5\uff0c\u539f\u7406\u548c\u4e0a\u9762\u4e00\u6837\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5b9e\u4f8b\u603b\u7ed3\u4e86PHP\u5b9e\u73b0\u4e8c\u7ef4\u6570\u7ec4\uff08\u6216\u591a\u7ef4\u6570\u7ec4\uff09\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u5206\u4eab\u7ed9\u5927\u5bb6\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5982\u4e0b\uff1a \u5047\u8bbe\u6709\u4e0b\u9762\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a \u73b0\u5728\u8981\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u6709\u4e24\u79cd\u60c5\u51b5\uff1a \u4e00\u79cd\u662f\u5c06\u6307\u5b9a\u5217\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u8fd9\u5728\u53e6\u4e00\u7bc7\u6587\u7ae0\u6709\u603b\u7ed3\uff1aPHP\u63d0\u53d6\u591a\u7ef4\u6570\u7ec4\u6307\u5b9a\u4e00\u5217\u7684\u65b9\u6cd5\u603b\u7ed3\u3002 \u73b0\u5728\u6211\u4eec\u91cd\u70b9\u8bb2\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u5c31\u662f\u628a\u6240\u6709\u7684\u503c\u90fd\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u800c\u4e14\u952e\u503c\u76f8\u540c\u4e0d\u4f1a\u88ab\u8986\u76d6\uff0c\u8f6c\u6362\u540e\u7684\u4e00\u7ef4\u6570\u7ec4\u662f\u8fd9\u6837\u7684\uff1a \u4e3b\u8981\u6709\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5\u3002 1 array_reduce\u51fd\u6570\u6cd5 \u7528array_reduce()\u51fd\u6570\u662f\u8f83\u4e3a\u5feb\u6377\u7684\u65b9\u6cd5\uff1a? \u56e0\u4e3aarray_merge\u51fd\u6570\u4f1a\u628a\u76f8\u540c\u5b57\u7b26\u4e32\u952e\u540d\u7684\u6570\u7ec4\u8986\u76d6\u5408\u5e76\uff0c\u6240\u4ee5\u5fc5\u987b\u5148\u7528array_value\u53d6\u51fa\u503c\u540e\u518d\u5408\u5e76\u3002 \u5982\u679c\u7b2c\u4e8c\u7ef4\u662f\u6570\u5b57\u952e\u540d\uff0c\u5982\uff1a \u90a3\u4e48\u76f4\u63a5\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\uff1a 2 array_walk_recursive\u51fd\u6570\u6cd5 \u7528array_walk_recursive()\u51fd\u6570\u5c31\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u628a\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u591a\u7ef4\u6570\u7ec4\uff1a \u7528\u8fd9\u4e2a\u65b9\u6cd5\u540e\u5c31\u53d8\u6210\uff1a 3 array_map\u51fd\u6570\u6cd5 \u7528array_map\u548carray_reduce\u51fd\u6570\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u5982\u4e0b\uff1a \u53ea\u662f\u9700\u8981\u591a\u58f0\u660e\u4e00\u4e2a\u7a7a\u7684$result\u6570\u7ec4\u3002 \u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u7528array_walk\u7684\u65b9\u6cd5\uff0c\u548cforeach\u5faa\u73af\u7684\u65b9\u6cd5\uff0c\u539f\u7406\u548c\u4e0a\u9762\u4e00\u6837\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-27"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}