如何从multidm数组键的开头删除字符

时间:2019-07-05 18:06:17

标签: php multidimensional-array

如何从此多维数组中的每个键中删除5_,6_,7 _....?我需要一个一致的多维数组,以便可以使用pdo更新表。

array (size=10)
  0 => 
    array (size=12)
      '5_pay_begin' => string '2019-07-02' (length=10)
      '5_pay_end' => string '2019-07-02' (length=10)
      '5_pay_date' => string '2019-07-02' (length=10)
      '5_check_date' => string '2019-07-02' (length=10)
      '5_week_number' => string '27' (length=2)
      '5_quarter' => string '1' (length=1)
      '5_pay_of_the_month' => string '2' (length=1)
      '5_weeks_worked' => string '2' (length=1)
      '5_pers_frequency_code' => string '4' (length=1)
      '5_pers_report_ending' => string '2019-07-02' (length=10)
      '5_pers_period_begin' => string '2019-07-02' (length=10)
      '5_pers_period_end' => string '2019-07-02' (length=10)
  1 => 
    array (size=12)
      '6_pay_begin' => string '2019-07-02' (length=10)
      '6_pay_end' => string '2019-07-02' (length=10)
      '6_pay_date' => string '2019-07-02' (length=10)
      '6_check_date' => string '2019-07-02' (length=10)
      '6_week_number' => string '27' (length=2)
      '6_quarter' => string '1' (length=1)
      '6_pay_of_the_month' => string '1' (length=1)
      '6_weeks_worked' => string '2' (length=1)
      '6_pers_frequency_code' => string '7' (length=1)
      '6_pers_report_ending' => string '2019-07-02' (length=10)
      '6_pers_period_begin' => string '2019-07-02' (length=10)
      '6_pers_period_end' => string '2019-07-02' (length=10)
  2 => 
    array (size=12)
      '7_pay_begin' => string '2019-07-02' (length=10)
      '7_pay_end' => string '2019-07-02' (length=10)
      '7_pay_date' => string '2019-07-02' (length=10)
      '7_check_date' => string '2019-07-02' (length=10)
      '7_week_number' => string '27' (length=2)
      '7_quarter' => string '1' (length=1)
      '7_pay_of_the_month' => string '1' (length=1)
      '7_weeks_worked' => string '2' (length=1)
      '7_pers_frequency_code' => string '7' (length=1)
      '7_pers_report_ending' => string '2019-07-02' (length=10)
      '7_pers_period_begin' => string '2019-07-02' (length=10)
      '7_pers_period_end' => string '2019-07-02' (length=10)

我尝试过

       foreach ($output as &$ytt){
       foreach ($ytt as $key => $value)
       {
        //   echo $key . '<br>';
           //echo strstr($key, '_') . '<br>';
           $output[$key] = substr(strstr($key, '_'), strlen('_'));
       }
    }

但这只会在数组内创建一个数组,而不会去除5_。

2 个答案:

答案 0 :(得分:1)

您需要unset旧密钥。

<?php
foreach ($output as &$ytt) {
    foreach ($ytt as $key => $value) {
        $newKey = substr(strstr($key, '_'), strlen('_'));
        $ytt[$newKey] = $value;
        unset($ytt[$key]);
    }
}

答案 1 :(得分:0)

使用terminal

的另一种方法
preg_replace()

工作演示: https://3v4l.org/blIP5