【问题标题】:Rename PHP array key and maintain element data重命名 PHP 数组键并维护元素数据
【发布时间】:2014-01-28 19:44:17
【问题描述】:

我正在尝试使用 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] => ) 
) 

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

【问题讨论】:

  • "使用 foreach 循环重命名我的所有数组键并取消设置。" --- 只需创建一个完整的 new 数组

标签: php arrays key rename unset


【解决方案1】:

以下将做你的行为:

$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() 组合您传递给它的第一个数组并将其分配为返回数组的键,并将第二个数组作为该数组的值传递给它。

【讨论】:

  • 您没有仔细查看 OP 的输入数组。第一个子数组中的键以_nl 结尾,第二个子数组中的键以_ds 结尾。
  • 没关系,他希望将初始数组中的所有键都更改为相同的键。
【解决方案2】:

使用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);

【讨论】:

    【解决方案3】:

    数组应该有相同数量的元素,但我认为这里就是这种情况。

    $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])
    ];
    

    【讨论】:

      【解决方案4】:

      递归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;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-12
        • 2015-12-28
        • 1970-01-01
        • 2021-05-25
        • 2015-06-09
        • 2017-10-19
        • 1970-01-01
        • 2017-06-20
        • 2012-09-16
        相关资源
        最近更新 更多