php - 从数组中提取数字
问题描述
如何从数组中只提取数字?
$myarray = array("A","B", "2","D");
我想从数组中获取数值(在本例中为“2”)到变量
解决方案
您可以使用is-numeric和array-filter(PHP 内置函数):
$myarray = array("A", "B", "2", "D", "3");
$b = array_filter($myarray, "is_numeric");
现在$b
是一个包含字符串的数组:2
和3
。
编辑关于您的评论:如果您只有 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
推荐阅读
- spring - docker 镜像正在运行,但无法在 Spring Boot 应用程序中访问 REST 端点
- mysql - 如何将表名、列名和值传递到 Mysql 中的 Storedprocedure
- docker - 运行 Meteor.js。“第一次跑步”是什么意思?
- mysql - 如何在restful api mysql中请求多个字段
- vue.js - vue-cli 如何从组件构建一个库并导入它?
- reactjs - 未经身份验证用户的反应路由器
- java - 使用 Java8 Stream API 合并两个 hashmap 列表
- java - 将指向 ArrayList 的 LinkedList 的指针转换为 String
- .net - 构建 dotnet vuejs 模板时出现“未知浏览器查询”错误
- javascript - 如何在悬停表格行上显示各个表格单元格的内容?