Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数组中获取URL变量_Php_Html_Arrays_Variables - Fatal编程技术网

Php 从数组中获取URL变量

Php 从数组中获取URL变量,php,html,arrays,variables,Php,Html,Arrays,Variables,我试图从数组中的对象列表中获取URL变量ID,并显示它们 当前代码: $display = print_r($array); echo $display; 输出: Array ( [0] => https://example.com?ID=435 [1] => https://example.com?ID=53 [2] => https://example.com?ID=5 [3] => https://example.com?ID=25 ) 我希望显示元素中数组

我试图从数组中的对象列表中获取URL变量
ID
,并显示它们


当前代码:

$display = print_r($array);
echo $display;
输出:

Array ( [0] => https://example.com?ID=435 [1] => https://example.com?ID=53 [2] => https://example.com?ID=5 [3] => https://example.com?ID=25 )

我希望显示
元素中数组中的每个URL变量
ID

<div>435</div>
<div>53</div>
<div>5</div>
<div>25</div>
435
53
5.
25
我已尝试使用以下代码执行此操作,但没有成功:

foreach($display as $display2) {
   echo '<div>'.$_GET['ID'].'</div>';
}
foreach($display为$display2){
回显“”。$\u获取['ID'.';
}

您要找的可能是这样的东西。您需要遍历数组中的每个URL并从中提取ID。这可以通过在
=
处拆分URL并抓取第二部分来实现

foreach($display as $display2) {
   $stringArray = explode("=", $display2); //Break apart string at "="
   $id = $stringArray[1]; //Grab second part of split string (the ID)
   echo '<div>'.$id.'</div>';
}
foreach($display为$display2){
$stringArray=explode(“=”,$display2);//在“=”处拆分字符串
$id=$stringArray[1];//获取分割字符串的第二部分(id)
回显'.$id'。';
}

您要找的可能是这样的东西。您需要遍历数组中的每个URL并从中提取ID。这可以通过在
=
处拆分URL并抓取第二部分来实现

foreach($display as $display2) {
   $stringArray = explode("=", $display2); //Break apart string at "="
   $id = $stringArray[1]; //Grab second part of split string (the ID)
   echo '<div>'.$id.'</div>';
}
foreach($display为$display2){
$stringArray=explode(“=”,$display2);//在“=”处拆分字符串
$id=$stringArray[1];//获取分割字符串的第二部分(id)
回显'.$id'。';
}

假设$array包含所有URL,如示例所示:

foreach($array as $v){
    $str = explode('=', $v);
    echo '<div>'.$str[1].'</div>';
}
foreach($v数组){
$str=爆炸('=',$v);
回显'.$str[1].';
}

假设$array包含所有URL,如示例所示:

foreach($array as $v){
    $str = explode('=', $v);
    echo '<div>'.$str[1].'</div>';
}
foreach($v数组){
$str=爆炸('=',$v);
回显'.$str[1].';
}
是一个超全局函数,包含所请求页面的所有查询参数。它不能用于url字符串列表

相反,您可以使用和的组合:

$url=array(
"https://example.com?ID=435",
"https://example.com?ID=53",
"https://example.com?ID=5",
"https://example.com?ID=25",
);
foreach($url作为$url){
if(strrpos($url,“?”)!==FALSE){
echo“”。substr($url,strpos($url,?“”+4)。“
”; } }
您可以将上面的示例复制并粘贴到中,以验证它是否按预期工作

编辑:

添加了
if
在echo周围阻塞,以过滤出可能没有
ID
值的URL。

是一个超级全局,包含所请求页面的所有查询参数。它不能用于url字符串列表

相反,您可以使用和的组合:

$url=array(
"https://example.com?ID=435",
"https://example.com?ID=53",
"https://example.com?ID=5",
"https://example.com?ID=25",
);
foreach($url作为$url){
if(strrpos($url,“?”)!==FALSE){
echo“.substr($url,strrpos($url,“?”)+4)。”
; } }
您可以将上面的示例复制并粘贴到中,以验证它是否按预期工作

编辑:


在echo周围添加了
if
块,以过滤出可能没有
ID
值的url。

超级全局
$\u GET
用于从当前请求中访问查询字符串值,而不是从存储在字符串中的任意url中读取它们

但不要担心,PHP的标准库有一些函数可以帮助您,即和

foreach($display as$url){
$queryString=parse\u url($url,PHP\u url\u QUERY);
parse_str($queryString,$parameters);
回显'.$parameters['ID'].';
}

超级全局$\u GET用于从当前请求中访问查询字符串值,而不是从存储在字符串中的任意url中读取它们

但不要担心,PHP的标准库有一些函数可以帮助您,即和

foreach($display as$url){
$queryString=parse\u url($url,PHP\u url\u QUERY);
parse_str($queryString,$parameters);
回显'.$parameters['ID'].';
}

这将涉及正则表达式(regex)。
$\u GET
变量来自当前页面的请求,不是从字符串中剥离查询数据的函数。这将涉及正则表达式(regex)。
$\u GET
变量来自当前页面的请求,不是从字符串中剥离查询数据的函数。我注意到代码中有
+4
。这就是数组中有多少个对象吗?我刚刚发布了一个例子;我无法预测将有多少对象(它会有所不同)不,应该从
substr()
操作开始的是超过查询字符串问号
的字符数。i、 e.在开始
substr()
操作之前,
substr()
函数应移动4个字符
?=ID
。这是一个偏移量,因为您知道查询字符串参数的名称。然而,如果发布的内容是一个例子,这将不起作用。如果URL总是有一个名为
ID
的参数,那么这是一种粗糙但有效的方法。诚然,如果URL只是示例,那么其他答案可能更合适。我注意到您代码中的
+4
。这就是数组中有多少个对象吗?我刚刚发布了一个例子;我无法预测将有多少对象(它会有所不同)不,应该从
substr()
操作开始的是超过查询字符串问号
的字符数。i、 e.在开始
substr()
操作之前,
substr()
函数应移动4个字符
?=ID
。这是一个偏移量,因为您知道查询字符串参数的名称。然而,如果发布的内容是一个例子,这将不起作用。如果URL总是有一个名为
ID
的参数,那么这是一种粗糙但有效的方法。诚然,如果URL只是示例,那么其他答案可能更合适。