[英]How to use str_replace() in a loop with multidimensional arrays in PHP?
I am working with three text files....... 我正在使用三个文本文件.......
space.txt space.txt
Kournikova Anna F F 6-3-1975 Red
Hingis Martina M F 4-2-1979 Green
Seles Monica H F 12-2-1973 Black
comma.txt comma.txt
Abercrombie, Neil, Male, Tan, 2/13/1943
Bishop, Timothy, Male, Yellow, 4/23/1967
Kelly, Sue, Female, Pink, 7/12/1959
pipe.txt pipe.txt
Smith | Steve | D | M | Red | 3-3-1985
Bonk | Radek | S | M | Green | 6-3-1975
Bouillon | Francis | G | M | Blue | 6-3-1975
and so far I have been able to parse them all into one array. 到目前为止,我已经能够将它们全部解析成一个数组。 The following output is this......
以下输出是......
Last_name, First_name, Middle_Initial, Gender, DOB, Fav_color
Kournikova Anna F F 6-3-1975 Red
Hingis Martina M F 4-2-1979 Green
Seles Monica H F 12-2-1973 Black
Abercrombie Neil Male Tan 2/13/1943
Bishop Timothy Male Yellow 4/23/1967
Kelly Sue Female Pink 7/12/1959
Smith Steve D M Red 3-3-1985
Bonk Radek S M Green 6-3-1975
Bouillon Francis G M Blue 6-3-1975
I have to replace all F's in Gender to Female, M's to Male, and the DOB format has to be "/". 我必须将性别中的所有F替换为女性,将M替换为男性,并且DOB格式必须为“/”。
Here is the following code I used to parsed the info: 以下是我用来解析信息的代码:
<?php
$space_txt = './data/input/space.txt';
$comma_txt = './data/input/comma.txt';
$pipe_txt = './data/input/pipe.txt';
$parsed_space_data = file_get_contents($space_txt);
$parsed_comma_data = file_get_contents($comma_txt);
$parsed_pipe_data = file_get_contents($pipe_txt);
$space_array = myExpldeLoopFunc('space', ' ', $parsed_space_data);
$comma_array = myExpldeLoopFunc('comma', ',', $parsed_comma_data);
$pipe_array = myExpldeLoopFunc('pipe', ' | ', $parsed_pipe_data);
$finalArray = array_merge($space_array, $comma_array, $pipe_array);
function myExpldeLoopFunc($name, $sep, $data)
{
$parsedData = explode("\r", $data);
$arr = [];
foreach ($parsedData as $data) {
$data_arr = explode($sep, $data);
if ($name == 'space') {
$arr[] = [
'last_name' => $data_arr[0],
'first_name' => $data_arr[1],
'middle_initial' => $data_arr[2],
'gender' => $data_arr[3],
'date_of_birth' => $data_arr[4],
'favorite_color' => $data_arr[5],
];
if ($data_arr[3] == 'F') {
return str_replace('F', 'Female', $arr);
} elseif ($name == 'comma') {
$arr[] = [
'last_name' => $data_arr[0],
'first_name' => $data_arr[1],
'gender' => $data_arr[2],
'date_of_birth' => $data_arr[3],
'favorite_color' => $data_arr[4],
];
} elseif ($name == 'pipe') {
$arr[] = [
'last_name' => $data_arr[0],
'first_name' => $data_arr[1],
'middle_initial' => $data_arr[2],
'gender' => $data_arr[3],
'favorite_color' => $data_arr[4],
'date_of_birth' => $data_arr[5],
];
}
}
return $arr;
}
}
The following result deletes 'Hingis Martina MF 4-2-1979 Green' and 'Seles Monica HF 12-2-1973 Black'. 以下结果删除了'Hingis Martina MF 4-2-1979 Green'和'Seles Monica HF 12-2-1973 Black'。 What would be the best solution to replace them all?
什么是最好的解决方案来取代它们?
I'd suggest to add two function to do the work. 我建议添加两个函数来完成工作。 One for renaming/normalizing the gender and one for normalizing the date.
一个用于重命名/标准化性别,一个用于标准化日期。 And then add a call to each of the function, when you process the data value in the loop.
然后在循环中处理数据值时,为每个函数添加一个调用。 In other words: you iterate over the values, modify them and store them in the final array.
换句话说:迭代值,修改它们并将它们存储在最终数组中。
function renameGender($gender)
{
if ($gender === 'F') {
return str_replace('F', 'Female', $gender);
}
if ($gender === 'M') {
return str_replace('M', 'Male', $gender);
}
}
function normalizeDate($date)
{
return str_replace('-', '/', $date);
}
function myExpldeLoopFunc($name, $sep, $data)
{
$parsedData = explode("\r", $data);
$arr = [];
foreach ($parsedData as $data) {
$data_arr = explode($sep, $data);
if ($name == 'space') {
$arr[] = [
"last_name" => $data_arr[0],
"first_name" => $data_arr[1],
"middle_initial" => $data_arr[2],
"gender" => renameGender($data_arr[3]),
"date_of_birth" => normalizeDate($data_arr[4]),
"favorite_color" => $data_arr[5]
];
} elseif ($name == 'comma') {
$arr[] = [
"last_name" => $data_arr[0],
"first_name" => $data_arr[1],
"gender" => renameGender($data_arr[2]),
"date_of_birth" => normalizeDate($data_arr[3]),
"favorite_color" => $data_arr[4]
];
} elseif ($name == 'pipe') {
$arr[] = [
"last_name" => $data_arr[0],
"first_name" => $data_arr[1],
"middle_initial" => $data_arr[2],
"gender" => renameGender($data_arr[3]),
"favorite_color" => $data_arr[4],
"date_of_birth" => normalizeDate($data_arr[5])
];
}
}
return $arr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.