从数组中获取ID(PHP)
我有这样一个数组:从数组中获取ID(PHP),php,arrays,drupal,ubercart,Php,Arrays,Drupal,Ubercart,我有这样一个数组: Array ( [0] => Array ( [0] => Sales [1] => Offices [2] => Products ) [1] => Array ( [0] => Cars [1] => Trucks [2] =
Array
(
[0] => Array
(
[0] => Sales
[1] => Offices
[2] => Products
)
[1] => Array
(
[0] => Cars
[1] => Trucks
[2] => Management
)
)
所有这些“汽车”、“卡车”等都是链接等。现在我需要从这些链接中获取的ID号?但是我对PHP还很陌生,我不知道如何获得它。Foreach循环和重置功能或类似功能
如果需要这些信息,可以在drupal和ubercart上查看
谢谢。像这样:
foreach($yourarry as $arr2){
foreach($arr2 as $id=>$text){
echo $id;
echo $text;
}
}
我认为您正在寻找一个带有preg_匹配并分解以提取ID的foreach循环:
$regex = '/href="([^"]+)/i';
foreach ($arr as $item) {
foreach ($item as $html) {
if (preg_match($regex, $html, $matches)) {
$id = end(explode('/', $matches[1]));
// For the string '<span class="field-content"><a href="/mysite/catalog/32">Cars/<a></span>' $id is equal to 32
}
}
}
a href=/mysite/catalog/63
…意味着63是汽车的id-但示例数组中既没有名为id的键,也没有63的值-您无法编写代码来查找不存在的值
如果要从链接文本中提取数字,请查看各种字符串解析函数,最简单的函数是strok、explode或preg_match。此时$text=。您需要explode或regex才能获取ID。谢谢,到目前为止,但xplode遇到了一些问题。我注意到它实际上也打印了一些html。基本上,$text是这样的:Cars和它的html:Cars/。我怎么能从那里得到32号身份证?谢谢你,克莱夫。这正是我想要的。现在有身份证了: