【问题标题】:Extracting numeric values from a text array从文本数组中提取数值
【发布时间】:2013-04-02 17:21:36
【问题描述】:

如何从这个数组中提取数值?

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

只需要去掉数字,然后我需要做一些计算。

【问题讨论】:

  • 你的预期输出是什么???
  • 两个字符串或两个整数,但只是数字,没有特殊字符。

标签: php arrays string int


【解决方案1】:
$source = array(
    0 => '\'268',
    1 => '252',
);

function strip($element)
{
    $matches = array();
    preg_match('#[0-9]+#', $element, $matches);
    return (int)reset($matches);
}

$result = array_map('strip', $source);

var_dump($result);

结果:

array (size=2)
  0 => int 268
  1 => int 252

【讨论】:

  • 谢谢。我做了漫长而艰难的方式,但这看起来更容易:)。我使用了 $tcx = (int)(trim($tc2[0],"'\'"); $tcy = (int)(trim($tc2[1],"'"); 并返回了两个字符串. 我假设我不需要在结束代码中做一个 var_dump($result) ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多