就像我已經在評論
print_r(array("\00foo\00bar" => 'works'));
作品說。但是,沒有任何理由使用implode
或serialize
或空字節鍵進行操作。
如果你想看到陣列是否相同,那麼你可以對它們進行比較:
$input1 = array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar');
$input2 = array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar');
var_dump($input1 === $input2);
將輸出true
而
$input3 = array('product_id' => 1, 'variation_id' => 2, 'foo' => 'foobarbaz');
var_dump($input1 === $input3);
會給false
。
引述PHP Manual on Array Operators:
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
而且,PHP具有用於從陣列中刪除重複的值的函數:
而當你的第二個參數設置爲SORT_REGULAR
,PHP會比較平等,例如數組
$data = array(
array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar'),
array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar'),
array('product_id' => 2, 'variation_id' => 2, 'foo' => 'bar')
);
print_r(array_unique($data, SORT_REGULAR));
將數組減少到只有第一個和第三個元素。
你能顯示代碼嗎? – evan
'print_r(array(「\ 00foo \ 00bar」=>'works'));'適用於我,是的,你應該使用另一種解決方案。如何使用多維數組呢?也許,如果你能解釋你想要達到的目標,我們可以找到更好的方法來解決這個問題。 – Gordon