我有一个数组$ indexedarray
printr($ indexedarray)给出了类似的东西
array (size=3)
0 => string 'Homes' (length=5)
1 => string 'Apartments' (length=10)
2 => string 'Villas' (length=6)
我想更改此数组索引也与值相同,如
array (size=3)
'Homes' => string 'Homes' (length=5)
'Apartments' => string 'Apartments' (length=10)
'Villas' => string 'Villas' (length=6)
是不可能的?
答案 0 :(得分:3)
您可以使用array_combine
:
$indexedarray= ['Homes', 'Apartments', 'Villas'];
print_r(array_combine($indexedarray, $indexedarray));
<强>给出:强>
Array
(
[Homes] => Homes
[Apartments] => Apartments
[Villas] => Villas
)
但请注意,您的重复值将被删除。钥匙将是独一无二的!
答案 1 :(得分:-1)
试试这个:
$myArray = [
0 => 'Homes',
1 => 'Apartments',
2 => 'Villas' ];
$newArray = [];
foreach($myArray as $key => $value){
$newArray[$value] = $value;
}
var_dump($newArray);