我有一个字符串,例如: Crosby Bing,Gretzky Wayne,Clemente Roberto
我想将字符串转换为数组,如下所示:
LName[0]=>Crosby
FName[0]=>Bing,
LName[1]=>Gretzky
FName[1]=>Wayne,
LName[2]=>Clemente
FName[2]=>Roberto
我对数组和字符串操作非常糟糕,并且在网上进行了很多搜索,但找不到合适的解决方案。
我期待您的回复!
答案 0 :(得分:2)
$nameString = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
$names = array_map('trim', explode(',', $nameString));
foreach ($names as &$name) {
$name = explode(' ', $name, 2);
}
它与你想要的略有不同。它是一个数组数组,其中内部数组有2个值,第一个是第一个,第二个值是第二个(以及任何其他)名称。如果需要的话,将它转换成你的结构应该不难。
echo $name[0][0]; // Crosby
echo $name[0][1]; // Bing
Kevin Peno在下面的评论中提出了类似的建议
$nameString = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
foreach (explode(',',$nameString) as &$name) {
$name = explode(' ', trim($name), 2);
}
然而,它们都是语义等价的,只要字符串中没有1M名称(这也会消耗“1M * average-string-length Byte”的内存),我不认为任何人都可能意识到任何性能差异
答案 1 :(得分:1)
完成一行!
preg_match_all('/(\w+) (\w+)(?:, )?/', "Crosby Bing, Gretzky Wayne, Clemente Roberto", $a);
/*
$fName = $a[1];
$lName = $a[2];
*/
答案 2 :(得分:1)
试试这个:
<?php
$LName = array();
$FName = array();
list($LName[0],$FName[0],$LName[1],$FName[1],$LName[2],$FName[2]) =
explode(" ","Crosby Bing, Gretzky Wayne, Clemente Roberto");
print_r($LName);
print_r($FName);
?>
演示:http://codepad.org/g4YnwKxW
或者如果您的列表是动态的:
<?php
$LName = array();
$FName = array();
$fullNames =
explode(", ","Crosby Bing, Gretzky Wayne, Clemente Roberto");
foreach($fullNames as $full){
list($LName[], $FName[]) = explode(" ", $full);
}
print_r($LName);
print_r($FName);
?>
答案 3 :(得分:1)
我认为explode()
可能很有用。
示例:
使用的东西:
explode()
var_dump()
$name = 'Crosby Bing';
$splittedName = explode(' ', $name);
var_dump($splittedName);
应该返回:
[0] => 'Crosby',
[1] => 'Bing'
分割名称列表只是简单的循环:
使用的东西:
explode()
foreach
reference
$names = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
$namesArray = explode(',', $names);
foreach($names as &$name)
{
$name = explode(' ', trim($name));
}
简单的功能:
使用的东西:
explode()
Function creation
count()
array_slice()
function GetNames($name)
{
$names = explode(' ', $name);
$namesCount = count($names);
$data = array();
$data['Names'] = array_slice($names, 0, $namesCount - 2);
$data['Surname'] = $names[$namesCount - 1];
return $data;
}
答案 4 :(得分:1)
您需要执行两项操作。首先将初始字符串拆分为一个名称数组,然后遍历该数组并将每个名称拆分为firstname和lastname。
以下代码显示,但仅在全名仅包含名字和姓氏且两者均由单个空格分配的前提下有效。
$allNames = 'Crosby Bing, Gretzky Wayne, Clemente Roberto'; // your string
$names = explode(', ', $allNames); // create an array with all names
$LName = $FName = array(); // initialize your result arrays
foreach($names as $name) {
list($firstName, lastName) = explode(' ', $name, 2); // split each name apart
$FName[] = $firstName; // add first name to result array
$LName[] = $lastName; // add last name to result array
}
Explode是你的朋友;)
答案 5 :(得分:0)
您可以使用explode功能,如下所示:
$names_string = "Crosby Bing, Gretzky Wayne, Clemente Roberto";
$names = explode(",", $names_string);
echo $names[0]; // displays Crosby Bing
echo $names[1]; // displays Gretzky Wayne
答案 6 :(得分:0)
<强>演示强>
<强>代码强>
$str = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
$arr1=explode(',', $str);
foreach ($arr1 as $item){
$arr2 = explode(' ', trim($item));
$lname[]=$arr2[0];
$fname[]=$arr2[1];
}
print_r($lname);
print_r($fname);
<强>结果强>
Array
(
[0] => Crosby
[1] => Gretzky
[2] => Clemente
)
Array
(
[0] => Bing
[1] => Wayne
[2] => Roberto