如何使用PHP从数组中分离值。以下是我的代码。如何使用PHP从数组中分离值
http://oditek.in/spesh/mobileapi/categoryproduct.php?item=1&acn=5&subcatid=a15,17,19
从上述URL我不得不提取subcatid
值和那些由具有在开头字母等a15
字母将移除和将保持恒定和所有ID将保存到另一个阵列。
如何使用PHP从数组中分离值。以下是我的代码。如何使用PHP从数组中分离值
http://oditek.in/spesh/mobileapi/categoryproduct.php?item=1&acn=5&subcatid=a15,17,19
从上述URL我不得不提取subcatid
值和那些由具有在开头字母等a15
字母将移除和将保持恒定和所有ID将保存到另一个阵列。
是你想要的是这样的:
<?php
$subcatid = explode(",",$_GET['subcatid']);
$arrId = [];
foreach($subcatid AS $id) {
$arrId[] = preg_replace("/[^0-9,.]/", "", $id);
}
var_dump($arrId);//$arrId contains all the numeric IDs only
?>
请像这样做:
<?php
$subcatids = = explode(",",$_GET['subcatid']);
$new_array = array();
foreach($subcatids as $subcatid) {
preg_match_all('!\d+!', $subcatid, $matches);
array_push($new_array, $matches[0][0]);
}
print_r($new_array);
?>
它提供了以下错误输出警告:无效的参数提供的foreach()在/var/www/oditek.in/public_html/spesh/mobileapi/categoryproduct.php在线330 Array()' – satya
$ subcatids是不是数组 –
进一步解释。什么确切你想要 – Monty
“*以下是我的代码*” - 这不是代码 - 你只是列出你想要的东西的数据。你有什么尝试过自己?如果您尝试过的唯一一件事情是将网址粘贴到您的代码中,然后放弃,那么您应该尝试*真实*尝试。 – h2ooooooo
在subcatid我有多个ids我需要那些单独内循环和那些有id的信件在这种情况下,信将删除。通过使用这些ID我需要从我的SQL表中获取数据。 – satya