{"id":182210,"date":"2024-05-08T21:15:39","date_gmt":"2024-05-08T13:15:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/182210.html"},"modified":"2024-05-08T21:15:47","modified_gmt":"2024-05-08T13:15:47","slug":"php%e5%a6%82%e4%bd%95%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84%e5%8e%bb%e9%87%8d%ef%bc%8c%e6%a0%b9%e6%8d%ae%e5%8f%98%e9%87%8f%e5%80%bc%e7%bb%84%e5%90%88%e5%95%86%e5%93%81%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/182210.html","title":{"rendered":"PHP\u5982\u4f55\u591a\u7ef4\u6570\u7ec4\u53bb\u91cd\uff0c\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u5546\u54c1\u5c5e\u6027"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27072239\/e487176c-2974-40f8-9390-902ded2b52b4.webp\" alt=\"PHP\u5982\u4f55\u591a\u7ef4\u6570\u7ec4\u53bb\u91cd\uff0c\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u5546\u54c1\u5c5e\u6027\" \/><\/p>\n<p><p><strong>PHP\u591a\u7ef4\u6570\u7ec4\u53bb\u91cd\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u6570\u7ec4\u5185\u90e8\u5143\u7d20\u7684\u552f\u4e00\u6027\u68c0\u6d4b\u3001\u4e5f\u53ef\u4ee5\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u751f\u6210\u5546\u54c1\u5c5e\u6027\u7684\u552f\u4e00\u6570\u7ec4\u3002<\/strong> \u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>array_map<\/code>\u548c<code>serialize<\/code>\u51fd\u6570\u5c06\u591a\u7ef4\u6570\u7ec4\u5e8f\u5217\u5316\u540e\u8fdb\u884c\u6bd4\u8f83\uff0c\u6216\u8005\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u53bb\u91cd\u51fd\u6570\u5229\u7528<code>array_search<\/code>\u548c<code>in_array<\/code>\u7b49\u51fd\u6570\u5b8c\u6210\u53bb\u91cd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u9488\u5bf9\u4e00\u4e2a\u5177\u4f53\u7684\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\uff0c\u5e76\u57fa\u4e8e\u8be5\u6570\u7ec4\u7684\u53d8\u91cf\u503c\u7ec4\u5408\u751f\u6210\u5546\u54c1\u7684\u5c5e\u6027\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u591a\u7ef4\u6570\u7ec4\u7684\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u53bb\u91cd\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u591a\u7ef4\u6570\u7ec4\u6709\u4e00\u4e2a\u6e05\u6670\u7684\u4e86\u89e3\u3002\u4e00\u4e2aPHP\u591a\u7ef4\u6570\u7ec4\u53ef\u80fd\u6709\u5982\u4e0b\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$products = array(<\/p>\n<p>    array(&#039;id&#039; =&gt; 1, &#039;name&#039; =&gt; &#039;\u5546\u54c1A&#039;, &#039;color&#039; =&gt; &#039;\u7ea2\u8272&#039;, &#039;size&#039; =&gt; &#039;M&#039;),<\/p>\n<p>    array(&#039;id&#039; =&gt; 2, &#039;name&#039; =&gt; &#039;\u5546\u54c1B&#039;, &#039;color&#039; =&gt; &#039;\u7eff\u8272&#039;, &#039;size&#039; =&gt; &#039;L&#039;),<\/p>\n<p>    \/\/ \u66f4\u591a\u5143\u7d20...<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u53bb\u91cd\uff0c\u6211\u4eec\u9700\u8981\u786e\u5b9a\u54ea\u4e9b\u5b57\u6bb5\u7684\u7ec4\u5408\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5546\u54c1\u7684\u552f\u4e00\u6807\u8bc6\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>id<\/code>\u662f\u552f\u4e00\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f9d\u636e<code>id<\/code>\u8fdb\u884c\u53bb\u91cd\uff1b\u5982\u82e5<code>id<\/code>\u53ef\u91cd\u590d\uff0c\u9700\u8981\u6839\u636e<code>name<\/code>\u3001<code>color<\/code>\u548c<code>size<\/code>\u7684\u7ec4\u5408\u6765\u5b9a\u4e49\u552f\u4e00\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u53bb\u91cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u53bb\u91cd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4e2d\u6211\u4eec\u5c06\u4e13\u6ce8\u4e8e\u627e\u51fa\u6570\u7ec4\u4e2d\u552f\u4e00\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">function unique_multidim_array($array, $key) {<\/p>\n<p>    $temp_array = array();<\/p>\n<p>    $i = 0;<\/p>\n<p>    $key_array = array();<\/p>\n<p>    foreach($array as $val) {<\/p>\n<p>        if (!in_array($val[$key], $key_array)) {<\/p>\n<p>            $key_array[$i] = $val[$key];<\/p>\n<p>            $temp_array[$i] = $val;<\/p>\n<p>        }<\/p>\n<p>        $i++;<\/p>\n<p>    }<\/p>\n<p>    return $temp_array;<\/p>\n<p>}<\/p>\n<p>$unique_products = unique_multidim_array($products, &#039;id&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u901a\u8fc7\u904d\u5386\u539f\u6570\u7ec4\uff0c\u68c0\u67e5<code>$key_array<\/code>\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e86<code>$key<\/code>\u503c, \u5982\u679c\u4e0d\u5b58\u5728\uff0c\u8868\u793a\u8be5\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u6570\u7ec4<code>$temp_array<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u53bb\u91cd\u65b9\u6cd5\u662f\u901a\u8fc7\u5e8f\u5217\u5316(<code>serialize<\/code>)\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b50\u6570\u7ec4\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u68c0\u67e5\u8fd9\u4e9b\u5b57\u7b26\u4e32\u662f\u5426\u6709\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">function serialize_unique_multidim_array($array) {<\/p>\n<p>    $serialized_array = array_map(&#039;serialize&#039;, $array);<\/p>\n<p>    $unique_array = array_unique($serialized_array);<\/p>\n<p>    return array_map(&#039;unserialize&#039;, $unique_array);<\/p>\n<p>}<\/p>\n<p>$unique_products = serialize_unique_multidim_array($products);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5e8f\u5217\u5316<\/strong>\u80fd\u591f\u786e\u4fdd\u5373\u4fbf\u662f\u590d\u6742\u7684\u591a\u7ef4\u6570\u7ec4\u4e5f\u53ef\u7ecf\u8fc7\u8f6c\u6362\u540e\u7528\u4e8e\u6bd4\u8f83\u3002\u53bb\u9664\u91cd\u590d\u540e\u518d\u6b21\u4f7f\u7528<strong>\u53cd\u5e8f\u5217\u5316<\/strong>\u65b9\u6cd5(<code>unserialize<\/code>)\u6062\u590d\u5176\u539f\u59cb\u7684\u6570\u7ec4\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u53d8\u91cf\u503c\u7ec4\u5408\u751f\u6210\u5546\u54c1\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8981\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u751f\u6210\u5546\u54c1\u5c5e\u6027\u7684\u552f\u4e00\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u5c06\u6bcf\u4e2a\u5546\u54c1\u7684\u5c5e\u6027\u7ec4\u5408\u6210\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u68c0\u67e5\u552f\u4e00\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">function combine_product_attributes($array, $attributes) {<\/p>\n<p>    $combination_array = array();<\/p>\n<p>    foreach ($array as $item) {<\/p>\n<p>        $combined_attr_value = &#039;&#039;;<\/p>\n<p>        foreach ($attributes as $attr) {<\/p>\n<p>            $combined_attr_value .= $item[$attr] . &#039;_&#039;;<\/p>\n<p>        }<\/p>\n<p>        $combination_array[$combined_attr_value] = $item;<\/p>\n<p>    }<\/p>\n<p>    return array_values($combination_array);<\/p>\n<p>}<\/p>\n<p>$combined_attributes = array(&#039;name&#039;, &#039;color&#039;, &#039;size&#039;);<\/p>\n<p>$unique_combinations = combine_product_attributes($products, $combined_attributes);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u6307\u5b9a\u7684\u5c5e\u6027\u8fdb\u884c\u7ec4\u5408\uff0c\u751f\u6210\u4e00\u4e2a\u4ee5\u5c5e\u6027\u7ec4\u5408\u5b57\u7b26\u4e32\u4e3a\u952e\u7684\u65b0\u6570\u7ec4\u3002\u6570\u7ec4\u7684\u503c\u4f1a\u88ab\u6700\u540e\u4e00\u6b21\u8be5\u952e\u51fa\u73b0\u7684\u5143\u7d20\u66ff\u6362\uff0c\u8fd9\u81ea\u7136\u5b9e\u73b0\u4e86\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9ad8\u7ea7\u6570\u7ec4\u51fd\u6570\u5e94\u7528<\/h3>\n<\/p>\n<p><p>PHP\u7684\u9ad8\u7ea7\u6570\u7ec4\u51fd\u6570\u5982 <code>array_reduce<\/code>, <code>array_filter<\/code> \u7b49\u4e5f\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\u5904\u7406\uff0c\u8fd9\u8981\u6c42\u5177\u5907\u4e00\u5b9a\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u601d\u7ef4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">function advanced_unique_multidim_array($array, $unique_keys) {<\/p>\n<p>    return array_values(<\/p>\n<p>        array_reduce($array, function ($carry, $item) use ($unique_keys) {<\/p>\n<p>            $unique = &#039;&#039;;<\/p>\n<p>            foreach ($unique_keys as $key) {<\/p>\n<p>                $unique .= $item[$key];<\/p>\n<p>            }<\/p>\n<p>            if (!isset($carry[$unique])) {<\/p>\n<p>                $carry[$unique] = $item;<\/p>\n<p>            }<\/p>\n<p>            return $carry;<\/p>\n<p>        }, array())<\/p>\n<p>    );<\/p>\n<p>}<\/p>\n<p>$unique_keys = array(&#039;name&#039;, &#039;color&#039;, &#039;size&#039;);<\/p>\n<p>$unique_products_advanced = advanced_unique_multidim_array($products, $unique_keys);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u51fd\u6570\u4e2d\uff0c<code>array_reduce<\/code>\u7528\u4e8e\u9010\u4e2a\u5904\u7406\u6570\u7ec4\u9879\u5e76\u5c06\u5176\u7d2f\u79ef\u5230\u76ee\u6807\u6570\u7ec4\u3002\u91cc\u9762\u7684<code>$unique<\/code>\u5b57\u7b26\u4e32\u662f\u6240\u6709\u5173\u952e\u5c5e\u6027\u7684\u62fc\u63a5\uff0c\u7528\u4e8e\u5224\u65ad\u662f\u5426\u5df2\u7ecf\u6709\u4e86\u8be5\u7ec4\u5408\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0<strong>PHP\u591a\u7ef4\u6570\u7ec4\u7684\u6709\u6548\u53bb\u91cd<\/strong>\uff0c\u4ee5\u53ca\u6839\u636e\u4e0d\u540c\u7684\u53d8\u91cf\u503c\u7ec4\u5408\u6765\u751f\u6210\u5546\u54c1\u5c5e\u6027\u7684\u552f\u4e00\u6570\u7ec4\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5546\u54c1\u5217\u8868\u548c\u786e\u4fdd\u6570\u7ec4\u4e2d\u6570\u636e\u7684\u552f\u4e00\u6027\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528PHP\u5bf9\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\uff1f<\/strong><\/p>\n<p>\u5728PHP\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528array_map\u51fd\u6570\u914d\u5408array_unique\u51fd\u6570\u6765\u5bf9\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u901a\u8fc7array_map\u51fd\u6570\u5c06\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u6bcf\u4e2a\u5b50\u6570\u7ec4\u4e0a\uff0c\u7136\u540e\u4f7f\u7528array_unique\u51fd\u6570\u5bf9\u6574\u4e2a\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u591a\u7ef4\u6570\u7ec4\u7684\u53bb\u91cd\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u5546\u54c1\u5c5e\u6027\uff1f<\/strong><\/p>\n<p>\u8981\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u5546\u54c1\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528PHP\u7684\u6570\u7ec4\u548c\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0d\u540c\u7684\u5546\u54c1\u5c5e\u6027\u5b58\u50a8\u5728\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u4e2d\uff0c\u6bcf\u4e2a\u5b50\u6570\u7ec4\u4ee3\u8868\u4e00\u4e2a\u5546\u54c1\u7684\u5c5e\u6027\u3002\u7136\u540e\u4f7f\u7528\u5d4c\u5957\u7684foreach\u5faa\u73af\u6765\u904d\u5386\u6bcf\u79cd\u5546\u54c1\u5c5e\u6027\u7684\u6240\u6709\u53ef\u80fd\u7ec4\u5408\uff0c\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u6700\u540e\u5f97\u5230\u6240\u6709\u53ef\u80fd\u7684\u5546\u54c1\u5c5e\u6027\u7ec4\u5408\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u591a\u7ef4\u6570\u7ec4\u53bb\u91cd\u548c\u5546\u54c1\u5c5e\u6027\u7ec4\u5408\u7684\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u63d0\u5230\u7684\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u591a\u7ef4\u6570\u7ec4\u53bb\u91cd\u548c\u5546\u54c1\u5c5e\u6027\u7ec4\u5408\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528array_column\u51fd\u6570\u6765\u63d0\u53d6\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5217\uff0c\u7136\u540e\u4f7f\u7528array_unique\u51fd\u6570\u5bf9\u63d0\u53d6\u540e\u7684\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\u3002\u5bf9\u4e8e\u5546\u54c1\u5c5e\u6027\u7684\u7ec4\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u5f0f\u6765\u751f\u6210\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u6bcf\u6b21\u9012\u5f52\u65f6\u9009\u62e9\u4e00\u4e2a\u4e0d\u540c\u7684\u5546\u54c1\u5c5e\u6027\u8fdb\u884c\u7ec4\u5408\u3002\u8fd9\u6837\uff0c\u5728\u9012\u5f52\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u5f97\u5230\u6240\u6709\u53ef\u80fd\u7684\u5546\u54c1\u5c5e\u6027\u7ec4\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PHP\u591a\u7ef4\u6570\u7ec4\u53bb\u91cd\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u6570\u7ec4\u5185\u90e8\u5143\u7d20\u7684\u552f\u4e00\u6027\u68c0\u6d4b\u3001\u4e5f\u53ef\u4ee5\u6839\u636e\u53d8\u91cf\u503c\u7ec4\u5408\u751f\u6210\u5546\u54c1\u5c5e\u6027\u7684\u552f\u4e00\u6570 [&hellip;]","protected":false},"author":3,"featured_media":182224,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/182210"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=182210"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/182210\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/182224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=182210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=182210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=182210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}