如何使用PHP从数组中分离值

如何使用PHP从数组中分离值,php,arrays,Php,Arrays,如何使用PHP从数组中分离值。下面是我的代码 http://oditek.in/spesh/mobileapi/categoryproduct.php?item=1&acn=5&subcatid=a15,17,19 从上面的URL中,我必须提取subcatid值,这些值以字母开头,如a15,字母将被删除并保持不变,所有id将保存到另一个数组中。请这样做: <?php $subcatids = = explode(",",$_GET['subcatid']); $new_a

如何使用PHP从数组中分离值。下面是我的代码

http://oditek.in/spesh/mobileapi/categoryproduct.php?item=1&acn=5&subcatid=a15,17,19

从上面的URL中,我必须提取subcatid值,这些值以字母开头,如a15,字母将被删除并保持不变,所有id将保存到另一个数组中。

请这样做:

<?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);
?>
<?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);
?>
<?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
?>

多解释。你真正想要的是我的代码——那不是代码——你只是列出了你想要的数据。你自己试过什么?如果您尝试过的唯一一件事是将URL粘贴到您的代码中,然后放弃,那么您应该进行实际尝试。在Subbatid中,我有多个id,我需要将它们分别添加到循环中的那些id中,在这种情况下,字母将被删除。通过使用这些id,我需要从我的sql表中获取数据。因此,您希望保存id 15,17和19?它给出了以下错误输出警告:在/var/www/oditek.in/public_html/spesh/mobileapi/categoryproduct.php第330行上为foreach提供的参数无效数组$subcatids不是数组您可以删除,。在preg_替换中,如果您只需要整数。很高兴能帮上忙:@Jencen:如果我想删除那些带有数字和rest的字母的值,那么该怎么做呢?很抱歉,我不确定我是否理解你的问题。你能举例说明吗?