PHP:如何为关联数组中的相同值获取相同索引?

PHP:如何为关联数组中的相同值获取相同索引?,php,sorting,indexing,associative-array,Php,Sorting,Indexing,Associative Array,我有这样一个关联数组: ID Val ---------- B 1 C 2 A 1 D 3 E 2 G 4 F 1 A,B,F : 0 C,E : 1 D : 2 G : 3 我使用asort($array)对数组进行排序,因此得到: ID Val ---------- A 1 B 1 F 1 C 2 E 2 D 3 G 4 然后,我可以使用“arra

我有这样一个关联数组:

ID   Val
----------
B     1
C     2
A     1
D     3
E     2
G     4
F     1
A,B,F : 0
C,E   : 1
D     : 2
G     : 3
我使用asort($array)对数组进行排序,因此得到:

ID   Val
----------
A     1
B     1
F     1
C     2
E     2
D     3
G     4
然后,我可以使用“array_search($id,array_keys($array))”找到一个项目的索引

A的索引为0,B为1,F为2,C为3,等等(我认为对于具有相同Val的项目,这是随机的)

但我希望具有相同Val的项目获得相同的索引,如下所示:

ID   Val
----------
B     1
C     2
A     1
D     3
E     2
G     4
F     1
A,B,F : 0
C,E   : 1
D     : 2
G     : 3

任何想法?

这将获得所有唯一值,按数字排序,并返回与
$array[$key]
相关的值的索引:

array_search( 
    // you're looking for this value
    $array[ $key ], 
    // you only want unique values here (and array_unique applies sort, so 
    // no worries there)
    array_unique( 
        // you don't need the keys here.
        array_values( $arr ), 
        // sorting numerically.
        SORT_NUMERIC ) );
调用函数:

如果您多次调用同一搜索,我可能会将其包装在一个类中:

class Unique_Value_Query
{
    private $vals;
    private $baseArr;

    public function __construct( array $arr = array() )
    {
       $this->setBaseArray( $arr );
    }

    public function setBaseArray( $ba )
    {
       if( count( $ba ) < 1 ) return;
       $this->baseArray = $ba;
       $this->vals = array_unique( array_values( $ba ) );
    }

    public function getBaseArray(){ return $this->baseArray; }

    public function getKeyIndex( $key )
    {
        return array_search( $this->baseArray[ $key ], $this->vals );
    }

}

$array = array( 'bar' => 0, 'foo' => 1, 'baz' => 1);

$q = new Unique_Value_Query( $array );
echo $q->getKeyIndex( 'foo' ); // 1
echo $q->getKeyIndex( 'bar' ); // 0
类唯一值\u查询
{
私家车$VAL;
私人$baseArr;
公共函数构造(数组$arr=array())
{
$this->setBaseArray($arr);
}
公共函数设置数组($ba)
{
如果(计数($ba)<1)返回;
$this->baseArray=$ba;
$this->vals=array_unique(array_值($ba));
}
公共函数getBaseArray(){return$this->baseArray;}
公共函数getKeyIndex($key)
{
返回数组搜索($this->baseArray[$key],$this->vals);
}
}
$array=array('bar'=>0,'foo'=>1,'baz'=>1);
$q=新的唯一值查询($array);
echo$q->getKeyIndex('foo');//1.
echo$q->getKeyIndex('bar');//0

这将获得所有唯一值,按数字排序,并返回与
$array[$key]相关的值的索引。

array_search( 
    // you're looking for this value
    $array[ $key ], 
    // you only want unique values here (and array_unique applies sort, so 
    // no worries there)
    array_unique( 
        // you don't need the keys here.
        array_values( $arr ), 
        // sorting numerically.
        SORT_NUMERIC ) );
调用函数:

如果您多次调用同一搜索,我可能会将其包装在一个类中:

class Unique_Value_Query
{
    private $vals;
    private $baseArr;

    public function __construct( array $arr = array() )
    {
       $this->setBaseArray( $arr );
    }

    public function setBaseArray( $ba )
    {
       if( count( $ba ) < 1 ) return;
       $this->baseArray = $ba;
       $this->vals = array_unique( array_values( $ba ) );
    }

    public function getBaseArray(){ return $this->baseArray; }

    public function getKeyIndex( $key )
    {
        return array_search( $this->baseArray[ $key ], $this->vals );
    }

}

$array = array( 'bar' => 0, 'foo' => 1, 'baz' => 1);

$q = new Unique_Value_Query( $array );
echo $q->getKeyIndex( 'foo' ); // 1
echo $q->getKeyIndex( 'bar' ); // 0
类唯一值\u查询
{
私家车$VAL;
私人$baseArr;
公共函数构造(数组$arr=array())
{
$this->setBaseArray($arr);
}
公共函数设置数组($ba)
{
如果(计数($ba)<1)返回;
$this->baseArray=$ba;
$this->vals=array_unique(array_值($ba));
}
公共函数getBaseArray(){return$this->baseArray;}
公共函数getKeyIndex($key)
{
返回数组搜索($this->baseArray[$key],$this->vals);
}
}
$array=array('bar'=>0,'foo'=>1,'baz'=>1);
$q=新的唯一值查询($array);
echo$q->getKeyIndex('foo');//1.
echo$q->getKeyIndex('bar');//0

Thanx,我不得不交换array\u unique和array\u值以获得连续索引。。。我说的对吗?Thanx,我不得不交换数组的唯一值和数组的值来获得连续的索引。。。我说得对吗?