[英]How can i change the key of the array with the value of the same array in PHP?
[英]How can I change array key for specific array value
我有一個這樣的多維數組:
Position 0: {id=5, name=john}
Position 1: {id=7, name=frank}
Position 2: {id=9, name=tina}
我想做的是:將每個位置鍵(0,1,2)更改為id值!
Position 5: {id=5, name=john}
Position 7: {id=7, name=frank}
Position 9: {id=9, name=tina}
我嘗試根據PHP Doc使用“ array_flip”。 但是我沒有成功! 但是我剛剛找到一維數組的參考。 像這樣:
<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped);
?>
任何想法? 謝謝。
一個簡單的單行代碼:
$new_array = array_combine(
array_column($your_array, 'id'), // these are keys
$your_array // these are values
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.