foreach循环的问题

发布于 2024-10-28 01:36:41 字数 927 浏览 0 评论 0原文

我编写了这段代码:

$matches = array();
preg_match_all('/"Type":".+?",/', $text, $matches);


foreach($matches[0] as $match) {
    if (isset($_GET['dvd']) && !empty($_GET['dvd'])) {
        $dvd = $_GET['dvd'];
        if (stripos($match, 'DVD') !== false) {
            $match = '';
        }
    }
    echo $match;
}

在这段代码中,我在 $text 中搜索单词“type”,并将整行及其前面的单词存储在数组中。然后我循环浏览每一个,看看是否有 DVD 这个词。如果是,则将其删除并显示一个空行。


现在我还想搜索假设制造商并将其显示在返回的每种类型下方。

所以它应该返回假设结果:

Type: HDD
Manufacturer: WD

Type: Flash Drive
Manufacturer: Transcend

Type: CD
Manufacturer: Sony

所以我尝试放置另一个 preg_match_all 表达式:

$anotherMatch = array();
preg_match_all('/"Manufacturer":".+?",/', $text, $anotherMatch);

并且我尝试将其与前面的 foreach 表达式与 && 结合起来。运算符但它不起作用。我还尝试了不同的 foreach 表达式,然后尝试了一种用于最后回显的表达式。但这也不起作用。

你能告诉我如何达到想要的结果吗?谢谢...

I made this code :

$matches = array();
preg_match_all('/"Type":".+?",/', $text, $matches);


foreach($matches[0] as $match) {
    if (isset($_GET['dvd']) && !empty($_GET['dvd'])) {
        $dvd = $_GET['dvd'];
        if (stripos($match, 'DVD') !== false) {
            $match = '';
        }
    }
    echo $match;
}

In this code i search for the word "type" in $text and it stores the whole line with the words ahead of it in the array. And then i loop through each one and see if the word DVD is there. If it is then, it removes it and displays a blank line.


Now I also want to search for suppose manufacturer and display it beneath every type returned.

So it should return suppose the result :

Type: HDD
Manufacturer: WD

Type: Flash Drive
Manufacturer: Transcend

Type: CD
Manufacturer: Sony

So I tried it with putting another preg_match_all expression:

$anotherMatch = array();
preg_match_all('/"Manufacturer":".+?",/', $text, $anotherMatch);

And I tried combining this with the previous foreach expression with an && operator but it didn't work. Also I tried different foreach expressions and then one for echoing at the end. but that also didn't work.

Can you tell me how to achieve the desired result. Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2024-11-04 01:36:41

给定像这样的源输入:

"Type":"HDD",
"Manufacturer":"WD",
"Other":"Nonsense",
"Type":"Flash Drive",
"Manufacturer":"Transcend",
"Other":"More nonsense",
"Type":"CD",
"Manufacturer":"Sony",
"Other":"Yet even more nonsense",

并期望像这样的输出:

Type: HDD
Manufacturer: WD

Type: Flash Drive
Manufacturer: Transcend

Type: CD
Manufacturer: Sony

您可以使用这个正则表达式:

/"(Type|Manufacturer)":"([^"]+?)",/

并像这样循环它:

preg_match_all('/"(Type|Manufacturer)":"([^"]+?)",/', $text, $matches);

foreach($matches[0] as $match => $line)
{
    if (!empty($_GET['dvd']) && stripos($matches[2], 'DVD') !== false)
    {
        continue;
    }
    echo $matches[1][$match] . ': ' . $matches[2][$match] . "\n";
}

虽然,我认为这不会完全按照您想要的方式进行。

Given source input like this:

"Type":"HDD",
"Manufacturer":"WD",
"Other":"Nonsense",
"Type":"Flash Drive",
"Manufacturer":"Transcend",
"Other":"More nonsense",
"Type":"CD",
"Manufacturer":"Sony",
"Other":"Yet even more nonsense",

And expecting output like this:

Type: HDD
Manufacturer: WD

Type: Flash Drive
Manufacturer: Transcend

Type: CD
Manufacturer: Sony

You could use this regular expression:

/"(Type|Manufacturer)":"([^"]+?)",/

And loop over it like this:

preg_match_all('/"(Type|Manufacturer)":"([^"]+?)",/', $text, $matches);

foreach($matches[0] as $match => $line)
{
    if (!empty($_GET['dvd']) && stripos($matches[2], 'DVD') !== false)
    {
        continue;
    }
    echo $matches[1][$match] . ': ' . $matches[2][$match] . "\n";
}

Although, I don't think that will do quite exactly what you want it to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文