gpt4 book ai didi

php - 以空格作为分隔符展开数组并将值推送到新数组

转载 作者:搜寻专家 更新时间:2023-10-31 21:30:08 26 4
gpt4 key购买 nike

我有一个 post 函数,它本身就是一个数组,post 包含数组形式的人的全名,例如:

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

我想遍历这个数组并使用空格作为分隔符分解每个键的值,这样我就可以将各个值发送到一个单独的数组以单独存储。

我目前拥有的:

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);
}
}

这会打印名字的第一个字母而不是单词

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

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

)

建议?

最佳答案

你让自己太难了。这就是您所需要的:

$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);
}

结果:

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)

关于php - 以空格作为分隔符展开数组并将值推送到新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30850000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com