0

我有一个数组,里面有多个数组。我正在尝试更改键 ["517f467ca2ec9"] ["517f467ca310c"]... 但是假设我不知道数组键文本,我正在使用下面的代码片段,它给了我一个错误未定义的偏移量: 1

array(74) {
  [0]=>
  array(9) {
    ["517f467ca2ec9"]=>
    string(0) ""
    ["517f467ca310c"]=>
    string(0) ""
    ["517f467ca321a"]=>
    string(0) ""
    ["517f467ca3320"]=>
    string(0) ""
    ["517f467ca3427"]=>
    string(0) ""
    ["517f467ca352a"]=>
    string(0) ""
    ["517f467ca3666"]=>
    string(0) ""
    ["517f467ca378d"]=>
    string(0) ""
    ["517f467ca3897"]=>
    string(0) ""
  }
  [1]=>
  array(9) {
    ["517f467ca2ec9"]=>
    string(0) ""
    ["517f467ca310c"]=>
    string(0) ""
    ["517f467ca321a"]=>
    string(0) ""
    ["517f467ca3320"]=>
    string(0) ""
    ["517f467ca3427"]=>
    string(0) ""
    ["517f467ca352a"]=>
    string(0) ""
    ["517f467ca3666"]=>
    string(0) ""
    ["517f467ca378d"]=>
    string(0) ""
    ["517f467ca3897"]=>
    string(0) ""
  } 

代码片段

foreach ($rows as $k=>$v){
   $rows[$k] ['running_days'] = $rows[$k] [0];
   unset($rows[$k][0]);
}
4

3 回答 3

2

请尝试此代码,它将对您有所帮助

function changeKey(&$data)
{
  foreach ($data as $key => $value)
  {
    // Convert key
    $newKey = 'any'; // new key goes here

    // Change key if needed
    if ($newKey != $key)
    {
      unset($data[$key]);
      $data[$newKey] = $value;
    }

    // Handle nested arrays
    if (is_array($value))
    {
      changeKey($data[$key]);
    }
  }
}

$test = array('foo' => 'bar', 'moreFoo' => array('more' => 'foo'));
changeKey($test);
print_r($test);
于 2013-04-30T04:42:11.063 回答
1

看来你有多维数组。你可以试试这个...

// array container
$records = 'Your array with key here';

// init new array container
$myarray = array();

foreach ($records as $items) {
    foreach ($items as $k => $v) {
        $myarray[$k]['running_days'] = $v;
    }
}

printr_r($myarray);
于 2013-04-30T05:00:32.243 回答
0
$rows[$k]['running_days'] = array_shift($rows[$k]);
于 2013-04-30T04:48:14.023 回答