2014-01-10 164 views
-1

我想用foreach循環統一重命名所有數組鍵,並取消設置。重命名PHP數組鍵並維護元素數據

陣列之前:

Array 
( [0] => Array ( 
     [store_nl] => Store One 
     [id_nl] => 123456 
     [title_nl] => Product One 
     [price_nl] => $9.00) 

    [1] => Array ( 

     [store_ds] => Store Two 
     [id_ds] => 789012 
     [title_ds] => Product Two 
     [price_ds] => $8.00) 
) 

的foreach使用取消設置:

陣列後
if(isset($data)){ 
    foreach ($data as $k=>$v) 
    { 
    //Store One (ds) 
     $data[$k]['Store'] = $data[$k]['store_ds']; 
     $data[$k]['ItemID'] = $data[$k]['id_ds']; 
     $data[$k]['Description'] = $data[$k]['title_ds']; 
     $data[$k]['Price'] = $data[$k]['price_ds']; 
     unset($data[$k]['store_ds']); 
     unset($data[$k]['id_ds']); 
     unset($data[$k]['title_ds']); 
     unset($data[$k]['price_ds']); 
    //Store Two (nl) 
     $data[$k]['Store'] = $data[$k]['store_nl']; 
     $data[$k]['ItemID'] = $data[$k]['id_nl']; 
     $data[$k]['Description'] = $data[$k]['title_nl']; 
     $data[$k]['Price'] = $data[$k]['price_nl']; 
     unset($data[$k]['store_nl']); 
     unset($data[$k]['id_nl']); 
     unset($data[$k]['title_nl']); 
     unset($data[$k]['price_nl']); 
    } 
} 

Array 
( [0] => Array ( 
     [Store] => Store One 
     [ItemID] => 123456 
     [Description] => Product One 
     [Price] => $9.00) 

    [1] => Array ( 
     [Store] => 
     [ItemID] => 
     [Description] => 
     [Price] =>) 
) 

所有數組鍵已被改變,但部分數據現在不見了?有人可以告訴我一個更好的方法來做到這一點沒有數據丟失?

+0

「重命名所有使用foreach循環和取消我的數組鍵。」 ---只需創建一個complretely ** new **數組而不是 – zerkms

回答

3

下面會做什麼你契稅:

$myArray = array(
    array(
     'store_ni' => 'Store One', 
     'id_ni' => 123456, 
     'title_ni' => 'Product One', 
     'price_ni' => '$9.00' 
    ), 
    array(
     'store_ds' => 'Store Two', 
     'id_ds' => 789012, 
     'title_ds' => 'Product Two', 
     'price_ds' => '$8.00' 
    ) 
);  


$newKeys = array('Store', 'ItemID', 'Description', 'Price');  

$result = array(); 
foreach($myArray as $innerArray) 
{ 
    $result[] = array_combine(
     $newKeys, 
     array_values($innerArray) 
    ); 
} 

array_combine()您傳遞給它並將其指定爲返回數組的鍵和第一陣列結合第二個數組作爲該數組的值傳遞給它。

+0

您沒有仔細查看OP的輸入數組。第一個子數組中的鍵以'_nl'結尾,第二個子數組中的鍵以'_ds'結尾。 – Asaph

+2

應該沒關係,他希望初始數組中的所有密鑰都更改爲相同的密鑰。 – BoxMan0617

0

使用array_flip()可以在鍵和值之間切換,然後輕鬆地運行數值並再次「翻轉」數組(返回到原始狀態)。它應該是這樣的:

$tmp_arr = array_flip($arr); 
$fixed_arr = array(); 
foreach($tmp_arr as $k => $v){ 
    if($val == "store_nl"){ 
     $fixed_arr[$k] = "Store"; 
    } 
    // etc... 
} 
// and now flip back: 
$arr = array_flip($fixed_arr); 
0

數組應該有相同數量的元素,但我認爲在這裏是這種情況。

$data = [[ 
    'store_nl' => 'Store One', 
    'id_nl' => 123456, 
    'title_nl' => 'Product One', 
    'price_nl' => '$9.00' 
], [ 
    'store_ds' => 'Store Two', 
    'id_ds' => 789012, 
    'title_ds' => 'Product Two', 
    'price_ds' => '$8.00' 
]]; 
$keys = ['Store', 'ItemID', 'Description', 'Price']; 
$data = [ 
    'nl' => array_combine($keys, $data[0]), 
    'ds' => array_combine($keys, $data[1]) 
]; 
0

遞歸PHP重命名鍵功能:

function replaceKeys($oldKey, $newKey, array $input){ 
    $return = array(); 
    foreach ($input as $key => $value) { 
     if ($key===$oldKey) 
      $key = $newKey; 

     if (is_array($value)) 
      $value = replaceKeys($oldKey, $newKey, $value); 

     $return[$key] = $value; 
    } 
    return $return; 
}