{"id":205309,"date":"2025-05-29T10:05:43","date_gmt":"2025-05-29T02:05:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205309\/"},"modified":"2025-05-29T10:05:43","modified_gmt":"2025-05-29T02:05:43","slug":"python%e5%a6%82%e4%bd%95%e6%a8%a1%e6%8b%9fphp%e7%9a%84array_column%e5%87%bd%e6%95%b0%e5%8a%9f%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205309\/","title":{"rendered":"Python\u5982\u4f55\u6a21\u62dfPHP\u7684array_column\u51fd\u6570\u529f\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u5982\u4f55\u6a21\u62dfPHP\u7684array_column\u51fd\u6570\u529f\u80fd\uff1f <\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173279129267484bfc95594.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c php array_column \u65b9\u6cd5\u7684\u529f\u80fd<\/strong><\/p>\n<p>\u5728 php \u4e2d\uff0carray_column \u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u4ece\u591a\u7ef4\u6570\u7ec4\u4e2d\u63d0\u53d6\u6307\u5b9a\u5217\u7684\u503c\u3002\u5728 python \u4e2d\uff0c\u6ca1\u6709\u76f4\u63a5\u5bf9\u5e94\u4e8e\u8fd9\u4e2a\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6216\u5b57\u5178\u63a8\u5bfc\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u63d0\u53d6\u7279\u5b9a\u5217\u7684\u503c<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u7c7b\u4f3c\u4e8ephp\u4e2d array_column \u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<pre>nested_list = [\n    {'id': 1, 'name': 'alice', 'age': 25},\n    {'id': 2, 'name': 'bob', 'age': 30},\n    {'id': 3, 'name': 'charlie', 'age': 35},\n]<\/pre>\n<p>\u4e3a\u4e86\u63d0\u53d6\u6240\u6709 id \u5217\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\uff1a<\/p>\n<pre>id_list = [item['id'] for item in nested_list]<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>[1, 2, 3]<\/pre>\n<p><strong>\u63d0\u53d6\u952e\u503c\u5bf9<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u4ece\u5217\u8868\u4e2d\u521b\u5efa\u5b57\u5178\uff0c\u5176\u4e2d\u6307\u5b9a\u5217\u7684\u503c\u7528\u4f5c\u5b57\u5178\u7684\u952e\u6216\u503c\uff1a<\/p>\n<pre>id_name_dict = {item['id']: item['name'] for item in nested_list}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>{1: 'alice', 2: 'bob', 3: 'charlie'}<\/pre>\n<p><strong>\u5c01\u88c5\u4e3a\u51fd\u6570<\/strong><\/p>\n<p>\u4e3a\u4e86\u66f4\u65b9\u4fbf\u5730\u91cd\u590d\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5c01\u88c5\u6210\u51fd\u6570\uff1a<\/p>\n<pre>def extract_column(data, column_name):\n    return [item[column_name] for item in data]\n\ndef extract_column_to_dict(data, key_column_name, value_column_name):\n    return {item[key_column_name]: item[value_column_name] for item in data}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u8f7b\u677e\u63d0\u53d6\u5217\u503c\u3001\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>id_list = extract_column(nested_list, 'id')\nid_name_dict = extract_column_to_dict(nested_list, 'id', 'name')\n\nprint(id_list)\n# \u8f93\u51fa\uff1a [1, 2, 3]\nprint(id_name_dict)\n# \u8f93\u51fa\uff1a {1: 'Alice', 2: 'Bob', 3: 'Charlie'}<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPython\u5982\u4f55\u6a21\u62dfPHP\u7684array_column\u51fd\u6570\u529f\u80fd\uff1f \u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u5982\u4f55\u6a21\u62dfPHP\u7684ar&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205309","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205309"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205309\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}