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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题