我想更改多维数组的键

时间:2014-12-03 18:32:58

标签: php multidimensional-array

array (size=4)
  0 => 
    array (size=9)
      'ref_id' => string '11' (length=2)
      'ref_own' => string 'kevin' (length=5)
      'ref_nom' => string 'Herbert' (length=7)
      'ref_ape' => string 'Guzman' (length=6)
      'ref_tel1' => string '12345678' (length=8)
      'ref_tel2' => string '87654321' (length=8)
      'ref_tel3' => string '' (length=0)
      'ref_who' => string 'Daniel Quevedo' (length=14)
      'ref_fecha' => string '2014-12-03 10:16:10' (length=19)
  1 => 
    array (size=9)
      'ref_id' => string '12' (length=2)
      'ref_own' => string 'kevin' (length=5)
      'ref_nom' => string 'Afid' (length=4)
      'ref_ape' => string 'Rivas' (length=5)
      'ref_tel1' => string '12345678' (length=8)
      'ref_tel2' => string '32132121' (length=8)
      'ref_tel3' => string '' (length=0)
      'ref_who' => string 'Rick Delgadillo' (length=15)
      'ref_fecha' => string '2014-12-03 10:16:10' (length=19)

我想改变这个键:

REF_ID ref_own ref_nom ref_ape ref_tel1 ref_tel2 ref_tel3 ref_who

我尝试使用array_push但没有发生任何事情...... 想法?

1 个答案:

答案 0 :(得分:1)

您可以像这样更改

  <?php
  // your data
  $array =  array(array(
        'ref_id' =>  '11',
        'ref_own' =>  'kevin',
        'ref_nom' =>  'Herbert',
        'ref_ape' =>  'Guzman',
        'ref_tel1' =>  '12345678' ,
        'ref_tel2' =>  '87654321',
        'ref_tel3' =>  '',
        'ref_who' =>  'Daniel Quevedo',
        'ref_fecha' =>  '2014-12-03 10:16:10'),
        array(
        'ref_id' =>  '12',
        'ref_own' =>  'something',
        'ref_nom' =>  'data',
        'ref_ape' =>  'Guzman',
        'ref_tel1' =>  '12345678' ,
        'ref_tel2' =>  '87654321',
        'ref_tel3' =>  '',
        'ref_who' =>  'Daniel Quevedo',
        'ref_fecha' =>  '2014-12-03 10:16:10'));

  // new keys
  $newKeys = ['ref_id' => 'Reference ID',
              'ref_own'=> 'Reference Owner',
              'ref_nom' => 'Reference Number',
              'ref_ape' => 'Reference APE',
              'ref_tel1' => 'Reference Tel1',
              'ref_tel2' => 'Reference Tel2',
              'ref_tel3' => 'Reference Tel3',
              'ref_who' => 'Reference who',
              'ref_fecha' => 'Reference fecha'];

  foreach ($array as $key => $value) {
    foreach($value as $k => $v){
      $array[$key][$newKeys[$k]] = $v;
      unset($array[$key][$k]);
     }
  }
  var_dump($array);

输出

  array (size=2)
    0 => 
      array (size=9)
        'Reference ID' => string '11' (length=2)
        'Reference Owner' => string 'kevin' (length=5)
        'Reference Number' => string 'Herbert' (length=7)
        'Reference APE' => string 'Guzman' (length=6)
        'Reference Tel1' => string '12345678' (length=8)
        'Reference Tel2' => string '87654321' (length=8)
        'Reference Tel3' => string '' (length=0)
        'Reference who' => string 'Daniel Quevedo' (length=14)
        'Reference fecha' => string '2014-12-03 10:16:10' (length=19)
    1 => 
      array (size=9)
        'Reference ID' => string '12' (length=2)
        'Reference Owner' => string 'something' (length=9)
        'Reference Number' => string 'data' (length=4)
        'Reference APE' => string 'Guzman' (length=6)
        'Reference Tel1' => string '12345678' (length=8)
        'Reference Tel2' => string '87654321' (length=8)
        'Reference Tel3' => string '' (length=0)
        'Reference who' => string 'Daniel Quevedo' (length=14)
        'Reference fecha' => string '2014-12-03 10:16:10' (length=19)