dongxie1907 2014-09-24 00:22
浏览 35
已采纳

从数组中剥离整数

I use the following code to strip the product number from a page url so that i can query a database to check for a custom canonical link.

$qs = explode('&',$_SERVER['QUERY_STRING']);
$prodid = explode('=',$qs[0]);  
$prod_ID = $prodid[count($prodid)-1];

As an example, $qs contains Array ( [0] => products_id=1121 [1] => cPath=180_182 [2] => )

I then decided i also needed to be able to specify custom canonicals for the categories as well so i added the following code:

$catid = explode('=',$qs[1]);
$lastnmbr = explode('_',$catid[1]);    
$cat_ID = $lastnmbr[count($lastnmbr)-1];

This gives me $prod_ID of 1121 and $cat_ID of 182.

However, here is the problem. If i drop back to the product list rather than a specific product page the array held in $qs becomes Array ( [0] => cPath=180_182 [1] => ) which means that

$catid = explode('=',$qs[1]);

is no longer the correct array position to collect the category id and in fact it is collected by

$prodid = explode('=',$qs[0]);

which makes all the following code think it's a product.

I think i need to somehow use the information held before the = symbol to ensure that if it says cPath it assigns it to $catid and if it says products_id it assigns it to $prodid.

Is there a "correct" way to do this? I did try making a new var of

$catchk = explode($qs[0], '=');

thinking this would return the data BEFORE the = symbol but it didn't so i obviously have the wrong end of the stick here.

To make the solution clear, i used the suggestion from Tyr (Thank you) and finished up with

$catid = $_GET['cPath'];
$lastnmbr = explode('_',$catid);    
$cat_ID = $lastnmbr[count($lastnmbr)-1];
$prod_ID = $_GET['products_id'];

It was necessary to be able to strip the set of numbers following the last _ (underscore) as subcategories are shown as 180_182 etc

  • 写回答

1条回答 默认 最新

  • duancuan6466 2014-09-24 00:28
    关注

    What if you trying $_GET['product_id'] and $_GET['cPath'] instead of $_SERVER['QUERY_STRING'] usage?

    You are avoiding the problems of having the correct array path.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料