PHP-如何在2D数组的一列中切换/反转值
我有一个分解的数组,我试图在特定列中反转名字和姓氏 以下是阵列:PHP-如何在2D数组的一列中切换/反转值,php,arrays,reverse,explode,Php,Arrays,Reverse,Explode,我有一个分解的数组,我试图在特定列中反转名字和姓氏 以下是阵列: array(729) { ["ID;Position;Name;ER;FF;GA"]=> array(24) { [0]=> string(3) "ID" [1]=> string(3) "Position" [2]=> string(4) "Name" [3]=> string(4) "ER" [4]=>
array(729) {
["ID;Position;Name;ER;FF;GA"]=>
array(24) {
[0]=>
string(3) "ID"
[1]=>
string(3) "Position"
[2]=>
string(4) "Name"
[3]=>
string(4) "ER"
[4]=>
string(6) "FF"
[5]=>
string(13) "GA"
}
["7702;Manager;Johnson, Bill;44.5;6T;406"]=>
array(24) {
[0]=>
string(4) "7702"
[1]=>
string(1) "Manager"
[2]=>
string(11) "Johnson, Bill"
[3]=>
string(3) "44.5"
[4]=>
string(4) "6T"
[5]=>
string(1) "406"
}
如您所见,我需要在每个第三个元素(索引[2])中翻转名字和姓氏。我想通过“,”分隔符(因为它总是fname,lname)分解第三个元素,然后使用array_reverse来反转它们。。。然后重建
$dump_array = explode(PHP_EOL, $dump);
foreach($dump_array as $line){
$temp[$line]=explode(';', $line);
}
foreach($temp as $ele){
var_dump($temp[$ele]);
$temp[$ele]=array_reverse($temp[$ele[2]]); #I need to do another explode (',') somewhere?
}
您希望执行
内爆
,而不是最后一行中的分解()
,或下面的示例演示代码:
foreach($dump as $line) {
$temp_str = $line[2];
$temp_str = explode(",", $temp_str);
$line[2] = $temp_str[1] . ", " . $temp_str[0];
}
它是如何工作的:对于每个数组,它将该数组挑选为
$line
,然后分解存储在$line[2]
中的字符串,并颠倒它们的顺序,用新的反转值替换$line[2]
,中间用逗号连接
$temp = array("asjd", "first , last", "asdjlakd");
foreach($temp as $ele){
if (strpos($ele, ",")!== false){
$data = explode(",", $ele);#I need to do another explode (',') somewhere?
$reverse_data=array_reverse($data);
print_r($reverse_data);
}
}
尝试并收到2个错误:警告:strpos()期望参数1为字符串,数组给定警告:explode()期望参数2为字符串,数组给定