大家好。有一个像这样的数组
$array = array (
0 =>
array (
'domain' => '.mydomain.com',
'expirationDate' => '1671845325,529',
'hostOnly' => false,
'httpOnly' => false,
'name' => 'SID',
'path' => '/',
'sameSite' => 'unspecified',
'secure' => false,
'session' => false,
'storeId' => '0',
'value' => '4wdY_czyOdfgd5hfgh34h870uuj7LRagEE53hV3_gghetrxC-717H5-p5ycfP_uJomw.',
'priority' => 'high',
),
1 =>
array (
'domain' => '.mydomain.com',
'expirationDate' => '1671845325,529',
'hostOnly' => false,
'httpOnly' => true,
'name' => '__Secure-3PSID',
'path' => '/',
'sameSite' => 'norestriction',
'secure' => true,
'session' => false,
'storeId' => '1',
'value' => '4wdY_czyKmbn75De2Z13H8iMhL8h870uuj7LRagEE53hV3_guZ2W7Qf-y8NKsjpLmuNkEAw.',
'priority' => 'high',
),
2 =>
array (
'domain' => '.mydomain.com',
'expirationDate' => '1671845325,529',
'hostOnly' => false,
'httpOnly' => false,
'name' => 'SID',
'path' => '/',
'sameSite' => 'unspecified',
'secure' => false,
'session' => false,
'storeId' => '0',
'value' => '4wdY_czyOdfgd5hfgh34h870uuj7LRagEE53hV3_gghetrxC-717H5-p5ycfP_uJomw.',
'priority' => 'high',
),
);
显然,在一个数组中,元素 0 和 2 是相等的(相同的数组)。过滤数组的最佳方法是什么,只留下唯一的元素?换句话说,把它带到表格中:
$array = array (
0 =>
array (
'domain' => '.mydomain.com',
'expirationDate' => '1671845325,529',
'hostOnly' => false,
'httpOnly' => false,
'name' => 'SID',
'path' => '/',
'sameSite' => 'unspecified',
'secure' => false,
'session' => false,
'storeId' => '0',
'value' => '4wdY_czyOdfgd5hfgh34h870uuj7LRagEE53hV3_gghetrxC-717H5-p5ycfP_uJomw.',
'priority' => 'high',
),
1 =>
array (
'domain' => '.mydomain.com',
'expirationDate' => '1671845325,529',
'hostOnly' => false,
'httpOnly' => true,
'name' => '__Secure-3PSID',
'path' => '/',
'sameSite' => 'norestriction',
'secure' => true,
'session' => false,
'storeId' => '1',
'value' => '4wdY_czyKmbn75De2Z13H8iMhL8h870uuj7LRagEE53hV3_guZ2W7Qf-y8NKsjpLmuNkEAw.',
'priority' => 'high',
)
);
内置php函数
$clean = array_unique ( $array );
在这里不起作用并给出错误:
<br />
<b>Notice</b>: Array to string conversion in <b>[...][...]</b> on line <b>52</b><br />
<br />
<b>Notice</b>: Array to string conversion in <b>[...][...]</b> on line <b>52</b><br />
<br />
<b>Notice</b>: Array to string conversion in <b>[...][...]</b> on line <b>52</b><br />
这个问题有解决方案吗?
要过滤数组中的唯一元素,您需要传递标志
SORT_REGULAR默认情况下使用它
SORT_STRING,因此您的示例中的每个元素都由解释器转换为字符串(错误分别表示),然后对其进行排序和比较。标记排序类型:
顺便说一句,该函数
array_unique在保留键的同时删除非唯一元素。这意味着数组中可能有空格。要解决此问题,请使用以下内容。编码: