我想獲得基於值的數組的關鍵。如何搜索唯一的數組鍵?
$array1=array(
'0'=>'test1',
'1'=>'test2',
'2'=>'test3',
'3'=>'test1'
)
$array2=array(
'0'=>'11',
'1'=>'22',
'2'=>'33',
'3'=>'44'
)
我
$source
是針。它可能是「test1
」,「test2
」或「test3
」
for loop to get different $source string
if(in_array($source[$i], $array1)){
$id=array_search($source[$i],$array1);
//I want to output 11, 22 or 33 based on $source
//However, my $array1 has duplicated value.
//In my case, if $source is test1, the output will be 11,11 instead of 11 and 44
echo $array2[$id]);
}
我不知道如何解決這個問題。我的大腦被炸。謝謝您的幫助!
感謝您的提示!但在閱讀array_flip()的手冊後,我會說array_flip對於大多數應用程序來說不是一個好主意,因爲只有字符串或數字被允許作爲值。然而,在**這個**的情況下,它會工作 – hek2mgl 2013-02-08 20:59:59
是的,但在這種情況下,這些值本身,是「字符串」。對於這個問題,它不適用於'Object'或'Arrays'數組。 – Achrome 2013-02-08 21:01:55