0

我有一个如下数组,我已将其放入一个名为 $testcoords 的数组中

array (
  0 => '\'263',
  1 => '252',)

我想提取数值并对其进行操作,然后将它们放回字符串中。我正在尝试使用以下代码:

$tc2 = explode(":",$testcoords);
$tcx = (int)(trim($tc2[0],"'\'");
$tcy = (int)(trim($tc2[1],"'");
$tcx2 = (($tcx)*(600/386));
$tcy2 = (($tcy)*(600/386));
$xtrans = (string)$tcx2;
$ytrans = (string)$tcy2;

到目前为止,我知道修剪函数有效 - 只是 (trim($tc2[0],"'\'") 或 (trim($tc2[1],"'") 以两个字符串返回我的数值。

现在我想做的是把这些数值转换为我试图与修剪函数结合的整数。一旦它们是整数,我想转回字符串并发布结果。

当我尝试这样做时,我没有得到任何结果。直到修剪数据的步骤很好。

例如,如果我只是这样做

$tcx = (trim($tc2[0],"'\'");
$tcy = (trim($tc2[1],"'");

对于上面列出的数组,并回显结果,我在回复中得到 263 和 252。

感谢有关如何完成其​​余部分的任何指示。

4

2 回答 2

0

这是一个语法错误。您没有认识到这一点,因为您禁用了错误报告。您可以使用 php.ini 设置display_errors=1log_errors=1error_reporting=E_ALL. 您也可以通过发出以下命令在脚本中执行此操作:

ini_set('display_errors', 1); // for development 
ini_set('display_errors', 0); // for production (display_errors would be a security risk)
ini_set('log_errors', 1); // for both development and production
ini_set('error_reporting', E_ALL);

错误:转换为 int 时缺少右括号。)

用这个:

$tcx = (int)(trim($tc2[0],"'\'")); // <-- note the second closing ')'
$tcy = (int)(trim($tc2[1],"'"));

进一步注意,这可以简化为

$tcx = (int)(trim($tc2[0],"'\'"); // <-- note the second closing ')'
$tcy = (int)(trim($tc2[1],"'");
于 2013-04-02T18:34:57.490 回答
0

您之前在这些行上有一个不必要且未闭合的括号trim

$tcx = (int)(trim($tc2[0],"'\'");
$tcy = (int)(trim($tc2[1],"'");

将这些更改为:

$tcx = (int)trim($tc2[0],"'\'");
$tcy = (int)trim($tc2[1],"'");

另外,在开发时打开error_reporting,你会看到这些错误被清楚地记录下来。

于 2013-04-02T18:35:26.633 回答