array(1) {
[0] => string(18) "AnotherTestSnippet"
}
array(1) {
[0] => string(17) "Test Code Snippet"
}
array(1) {
[0] => string(18) "AnotherTestSnippet"
}
array(1) {
[0] => string(17) "Test Code Snippet"
}
Run Code Online (Sandbox Code Playgroud)
如何使用PHP将上面的数组转换为这种格式?
array("AnotherTestSnippet","Test Code Snippet")
Run Code Online (Sandbox Code Playgroud)
那就是清理并删除重复项.我尝试过array_unique和in_array,但它不起作用.谢谢.
Let's call your arrays $array1 through $array4. Here is the solution:
$cleanArray = array_unique(array_merge($array1, $array2, $array3, $array4));
Run Code Online (Sandbox Code Playgroud)
EDIT:
Now that I know you are starting with a single, multi-dimensional array, this is not the correct answer. The correct answer is the for loop given by Dainis Abols.
只需执行一个循环并仅保存您的唯一条目:
<?php
$array = array (
array( "AnotherTestSnippet" ),
array( "Test Code Snippet" ),
array( "AnotherTestSnippet" ),
array( "Test Code Snippet" )
);
$new_array = array();
foreach ( $array as $value )
{
if( !in_array( $value[0], $new_array) ) $new_array[] = $value[0];
}
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => AnotherTestSnippet
[1] => Test Code Snippet
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |