首页 > 解决方案 > 从数组中提取数字

问题描述

如何从数组中只提取数字?

$myarray = array("A","B", "2","D");

我想从数组中获取数值(在本例中为“2”)到变量

标签: phparrays

解决方案


您可以使用is-numericarray-filter(PHP 内置函数):

$myarray = array("A", "B", "2", "D", "3");
$b = array_filter($myarray, "is_numeric");

现在$b是一个包含字符串的数组:23

编辑关于您的评论:如果您只有 1 个值并且您想添加10它,您可以执行以下操作:

$myarray = array("A", "B", "2", "D");
$b = array_filter($myarray, "is_numeric");
$c = array_values($b); //reset the keys
$finalvalue = $c[0] + 10; // will output 12

推荐阅读