-1
我有一个这样的数组:查找字符串中一个字
[
"customer" => 2325
"product1" => 3,
"product4" => 1,
"product12" => 2
]
我想运行一个for循环查找包含单词产品密钥并同时获得后的数字。有没有任何PHP函数来查找字符串中的特定单词?
我有一个这样的数组:查找字符串中一个字
[
"customer" => 2325
"product1" => 3,
"product4" => 1,
"product12" => 2
]
我想运行一个for循环查找包含单词产品密钥并同时获得后的数字。有没有任何PHP函数来查找字符串中的特定单词?
你可以通过你的阵列有可能循环使用时stristr
:
foreach($arr as $item => $val) {
$res[] = stristr($item, 'product');
$int[] = filter_var($item, FILTER_SANITIZE_NUMBER_INT);
}
print_r(array_filter($res)); // output keys with substring 'product'
print_r(array_filter($int)); // output integers from product key substring
这将允许您收集包含子串“产品”的任何按键。从那里你还可以捕获与该键相关的任何整数。
输出:
Array
(
[1] => product1
[2] => product4
[3] => product12
)
Array
(
[1] => 1
[2] => 4
[3] => 12
)
相关:http://stackoverflow.com/questions/2471120/php-function-array-key-exists-and-regular-expressions – jeremy
'strpos'是这个函数 –
可能重复[检查字符串是否包含特定单词?](http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words) – pBuch