将阵列从substr_replace

问题描述 投票:0回答:1
if ($rows[1] == 29) { $rows[1] = substr_replace("29","Sharpshooter",0); }

below是我的完整代码,我添加了更多有关我所说的示例。

<?php /* Link DB */ include_once 'config.php'; /* Initialize Connection */ $conn=sqlsrv_connect($serverName, $conn_array); if ($conn){ }else{ die; } /* Prepare Statement Preparation */ $sql = " SELECT TOP 25 G.CharacterName, G.JobCode, D.PVPWin, D.PVPLose, G.PvPExp, D.PVPGiveUp FROM PvPRanking as G INNER JOIN PVPScores as D ON G.CharacterID = D.CharacterID ORDER BY RANK() OVER (ORDER BY TotalRank ASC ) "; /* Assign Parameter values. */ $param1 = 1; $param2 = 2; // Array requirement for prepare statement. $procedure_params = array( array(&$param1, SQLSRV_PARAM_OUT), array(&$param2, SQLSRV_PARAM_OUT) ); /* The ACTUAL Prepare Statement */ $stmt = sqlsrv_prepare( $conn, $sql, $procedure_params); /*Execute*/ sqlsrv_execute($stmt); /* Variables */ $autoincrement = 0; echo ' <tr> <th>Rank</th> <th>Name</th> <th>Class</th> <th>Wins</th> <!-- <th>Losses</th> <th>Experience</th> <th>Quit</th> --> </tr>'; // Rank # to increment itself. I.E 1,2,3,4,5,6 etc.. while ($rows=sqlsrv_fetch_array($stmt)) { $autoincrement++; { if ($rows[1] == 29) { $rows[1] = substr_replace("29","Sharpshooter",0); } if ($rows[1] == 30) { $rows[1] = substr_replace("30","Knight",0); } if ($rows[1] == 29) { $rows[1] = substr_replace("31","Dragon Slayer",0); } } // Echo will spit out the rows and the data. echo ' <tr id=io> <td> '.$autoincrement.' </td> <b> <td style = "color:#AFA;"> '.$rows[0].' </td> </b> <td> '.$rows[1].' </td> <td> '.$rows[2].' </td> <!-- <td> '.$rows[3].' </td> <td> '.$rows[4].' </td> <td> '.$rows[5].' </td> --> '; } ?> </table>

创建了自己的翻译阵列。  如果该值不是数组中的键,请不要替换它。

$replacements = [ 29 => 'Sharpshooter', ]; $rows[1] = $replacements[$rows[1]] ?? $rows[1];

用循环使用循环,应在循环之前写入翻译数组。
在线,所有这些都可以在SQL中执行。
php arrays replace mapping
1个回答
1
投票

最终,我建议您避免在查询的每个调用中执行此操作。换句话说,任何表具有列列列为唯一值的值 - 该表应接收新列。

如果您没有包含这些唯一作业码的表,那么您应该制作一个表并将该表与现有查询一起加入。 这是最专业和可维护的解决方案,因为当您需要更新列表时,您只需要更新表即可更新项目中的潜在多重文件。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.