仅从数组中获取某些值

时间:2014-02-27 09:11:15

标签: php arrays

我有一个包含以下值的数组

    array (size=7)
  0 => string '020120140759' (length=12)
  1 => string '020120140759' (length=12)
  2 => string '020120140759' (length=12)
  3 => string '020220140759' (length=12)
  4 => string '020220140759' (length=12)
  5 => string '020320140759' (length=12)
  6 => string '020320140759' (length=12)

您会注意到某些数字的值是相同的,我想提取数组中出现的每种数据的最后一个值,以便新数组看起来像这样。

array (size=2)

  2 (this will change to 0 ) => string '020120140759' (length=12)
  6 (this will change to 1 ) => string '020320140759' (length=12)

我尝试过很多东西,但我没有成功。任何帮助都会非常感激

3 个答案:

答案 0 :(得分:2)

array_unique是要走的路。

var_dump( array_unique( array_reverse($yourOriginalArrayHere) ) );

答案 1 :(得分:0)

您可以使用array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

排序标志将是

SORT_REGULAR
SORT_NUMERIC
SORT_STRING
SORT_LOCALE_STRING

示例

$result = array_unique($input);
print_r($result);

答案 2 :(得分:0)

<?

//first step : Exchanges all keys with their associated values in an array $newArray
//second step: get $newArray's key
[array_flip][1]
[array_keys][2]

$array = array(
  0 =>  '020120140759' ,
  1 =>  '020120140759' ,
  2 =>  '020120140759' ,
  3 =>  '020220140759' ,
  4 =>  '020220140759' ,
  5 =>  '020320140759' ,
  6 =>  '020320140759' 
 );

$newArray = array_keys( array_flip($array) );
echo '<pre>';  
var_dump($newArray);
echo '</pre>';