drjyvoi734793 2015-06-15 16:12
浏览 34
已采纳

爆炸数组,以空格作为分隔符并将值推送到新数组

I have post function that is an array in itself, the post contains full names of people in the form of an array such as:

Array(
  [0] => Adda Gweno
  [1] => Tom malombo
)

I would like to go through this array and explode the values of each key using the space as a delimiter so I can send the individual values to a separate array to be stored separately.

What I have so far:

function save_drs() {    
    $Surname = $this->input->post('Surname'); 
    foreach ($Surname as $e_name):
        $individual = explode(" ", $e_name);
    endforeach;

    $Suggested_Salary = $this->input->post('Suggested_Salary');
    for ($i = 0; $i < count($Surname); $i++) {         
        $bio = array(
            'Surname' =>$individual[0][$i],
            'Other_Names' => $individual[1][$i],
            'Suggested_Salary'=>$Suggested_Salary[$i]
           );
            // $this->db->insert('main_table', $bio);
        print_r($bio);
    }
}

This prints the first letters of the names instead of the words

Array
(   
    [Surname] => A      //Should be Adda
    [Other_Names] => G  //Should be Gweno

)
Array
(   
    [Surname] => T      //Should be Tom
    [Other_Names] => M  //Should be Malombo

)

Suggestions?

  • 写回答

2条回答 默认 最新

  • download201401 2015-06-15 16:27
    关注

    You are making it too hard for yourself. This is all you need:

    $employees = array("Adda Gweno", "Tom malombo");
    $salaries = array("500", "355");
    foreach ($employees as $k => $fullname) {
        $names = explode(" ", $fullname);
        $final["Surname"] = $names[0];
        $final["Other_Names"] = $names[1];
        $final["Salaries"] = $salaries[$k];
        var_dump($final);
    }
    

    Result:

    array (size=3)
      'Surname' => string 'Adda' (length=4)
      'Other_Names' => string 'Gweno' (length=5)
      'Salaries' => string '500' (length=3)
    array (size=3)
      'Surname' => string 'Tom' (length=3)
      'Other_Names' => string 'malombo' (length=7)
      'Salaries' => string '355' (length=3)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失