You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在给定PHP数组中匹配指定文本Trykk front?

如何在PHP数组中匹配文本“Trykk front”?

给定如下PHP数组结构(通过var_dump($haystack)输出):

array(2) {
    [0] => array(11) {
        ["id"] => string(13) "64085aafcb9db"
        ["type"] => string(10) "checkboxes"
        ["raw"] => array(1) {
            [2] => string(5) "b3ygo"
        }
        ["values"] => array(1) {
            [0] => array(6) {
                ["label"] => string(11) "Trykk front"
                ["price"] => float(42)
                ["price_type"] => string(2) "qt"
                ["slug"] => string(5) "b3ygo"
                ["calc_price"] => float(42)
                ["pricing_hint"] => string(23) "(+kr 42)"
            }
        }
        ["clone_type"] => string(0) ""
        ["label"] => string(31) "Ønsker du trykk på produktet?"
        ["hide_cart"] => bool(false)
        ["hide_checkout"] => bool(false)
        ["hide_order"] => bool(false)
        ["clone_idx"] => int(0)
        ["calc_weight"] => bool(false)
    }
    [1] => array(11) {
        ["id"] => string(13) "64085aaf29908"
        ["type"] => string(4) "file"
        ["raw"] => string(0) ""
        ["values"] => array(1) {
            [0]=>array(4) {
                ["label"] => string(0) ""
                ["price"] => int(0)
                ["price_type"] => string(4) "none"
                ["formatted_label"]=>string(31) ""
            }
        }
        ["clone_type"] => string(0) ""
        ["label"] => string(56) "Om du ønsker trykk på produktet laster du opp logo her"
        ["hide_cart"] => bool(false)
        ["hide_checkout"] => bool(false)
        ["hide_order"] => bool(false)
        ["clone_idx"] => int(0)
        ["calc_weight"] => bool(false)
    }
}

你当前尝试的代码无法得到预期结果:

$matches  = preg_grep ('/^Trykk front (\w+)/i', $haystack);

问题分析

  • preg_grep仅支持处理一维数组,没法直接遍历这种多层嵌套的数组结构。
  • 正则表达式写法错误:目标文本就是Trykk front,没必要加(\w+),这个表达式会匹配以Trykk front开头且后续带字母数字的字符串,和实际需求不符。

解决方案

方法1:直接遍历固定结构的数组

如果数组结构固定,可通过键名逐层访问判断:

$target = 'Trykk front';
$found = false;

// 遍历外层数组
foreach ($haystack as $item) {
    if (isset($item['values']) && is_array($item['values'])) {
        // 遍历values下的子项
        foreach ($item['values'] as $value) {
            if (isset($value['label']) && $value['label'] === $target) {
                $found = true;
                // 找到后直接跳出多层循环
                break 2;
            }
        }
    }
}

var_dump($found); // 找到则输出bool(true)

方法2:递归查找(适配不确定深度的数组)

如果数组结构可能变化,写一个递归函数遍历所有层级:

function findTargetInArray($array, $target) {
    foreach ($array as $val) {
        if (is_array($val)) {
            // 递归遍历子数组
            if (findTargetInArray($val, $target)) {
                return true;
            }
        } else {
            // 匹配目标文本
            if ($val === $target) {
                return true;
            }
        }
    }
    return false;
}

// 调用函数查找
$target = 'Trykk front';
$found = findTargetInArray($haystack, $target);
var_dump($found);

方法3:转字符串后正则匹配(不推荐)

如果只是临时验证,可以把数组转成字符串后用正则匹配,但容易出现误判(比如其他字段包含相同文本):

$target = 'Trykk front';
$arrayStr = print_r($haystack, true);
$found = preg_match("/\Q{$target}\E/", $arrayStr);
var_dump($found); // 找到则输出int(1),未找到则输出int(0)

内容的提问来源于stack exchange,提问作者Ixto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 16:52:15