これが私のコードです。それから私が問題を抱えていることを説明します:
foreach($people as $person){
// counter for each person
$counter = 0;
// get variables for each item
list($fullName,$phoneNumber,$address,$birthday,$salary) = explode(':', $person);
// get variables for first and last name
list($firstName, $lastName) = explode(" ", $fullName);
// get variables for phone numbers
list($areaCode, $middleDigits, $lastDigits) = explode('-',$phoneNumber);
//get variables for address
list($street,$city,$statezip) = explode(", ", $address);
// get variables for state and zip separately
list($state,$zipCode) = explode(" ", $statezip);
// print file with the first and last names reversed
$tempFName = $firstName;
$tempLName = $lastName;
echo $newPerson = (preg_replace("/($tempLName)($tempFName)/", $firstName, $lastName, $person))."<br/>";
$counter++;
}
私がやりたいのは、元の$ personを印刷することですが、$firstNameと$lastNameは逆になっています。値を置き換えてから各変数を出力することはできますが、各行をフォーマットしない限り、$personが最初に行ったのと同じフォーマットにはなりません。元の$person変数と同じように各出力をフォーマットせずにそれを行う方法があるかどうか疑問に思いました。
ありがとう!