重命名PHP数组键并维护元素数据

时间:2014-01-10 00:30:52

标签: php arrays key rename unset

我正在尝试使用foreach循环统一重命名所有数组键并取消设置。

之前的数组:

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] => ) 
) 

所有数组键都已更改,但部分数据现已消失?有人可以告诉我一个更好的方法来做到这一点而不会丢失数据吗?

4 个答案:

答案 0 :(得分: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()组合传递给它的第一个数组,并将其指定为返回数组的键,将第二个数组作为该数组的值传递给它。

答案 1 :(得分: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);

答案 2 :(得分: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])
];

答案 3 :(得分: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;
}