Foreach循环只处理最后一项



我要做的是在注释中找到URL,然后对照数组检查这些URL。如果URL在数组中,并且具有具有特定值的关键字"data",那么我希望用自定义文本替换注释中的每个URL。

除了更换零件外,我什么都能用。这就是我到目前为止所拥有的。

评论

Lorem www.link1.com and ipsum dolor sit amet, http://link2.com consectetur
adipiscing elit. Maecenas http://link3.com eu tempor nibh.

函数使用的数组

Array
(
    [0] => Array
        (
            [status] => 200
            [data] => one
            [url] => www.link1.com
        )
    [1] => Array
        (
            [status] => 204
            [data] => 
            [url] => http://link2.com
        )
    [2] => Array
        (
            [status] => 200
            [data] => two
            [url] => http://link3.com
        )
)

功能

function check_the_status( $arrays, $comment ) {
    // Bad statis types
    $status = apply_filters( 'status_filter', array( 'one', 'two', 'three' ) );
    $modified = '';
    foreach( $arrays as $key => $array ){
        $url = $array['url'];
        $data = $array['data'];
        if( in_array( $data, $status ) ) {
            $modified = '<span>the new text</span>';
            $the_comment = str_replace( $url, $modified, $comment );
        }
    }
    return $the_comment;
}

输出

Lorem www.link1.com and ipsum dolor sit amet, http://link2.com consectetur
adipiscing elit. Maecenas "<span>the new text</span>" eu tempor nibh.

所以,这个函数的作用是,它只将新文本应用于数组中的最后一个URL,但我希望它应用于第一个和最后一个网址,因为它们都有我在数组中检查的数据值。我认为前臂循环不是实现我想要的目标的正确方法,但我不确定什么是最好的方法。

问题出现在以下行。通过每次传递$comment来重新开始每次迭代:

$the_comment = str_replace( $url, $modified, $comment );

您应该将其更改为:

$the_comment = str_replace( $url, $modified, $the_comment );

并在循环之前初始化$the_comment:

$the_comment = $comment;

基于其他注释,以下是工作代码。我添加了"粗体"样式,所以结果更加明显。

    $arrays = Array
    (
        0 => Array
            (
                'status' => 200,
                'data' => 'one',
                'url' => 'www.link1.com'
            ),
        1 => Array
            (
                'status' => 204,
                'data' => '',
                'url' => 'http://link2.com'
            ),
        2 => Array
            (
                'status' => 200,
                'data' => 'two',
                'url' => 'http://link3.com'
            )
    );
    function check_the_status( $arrays, $comment ) {
        // Bad statis types
        $status = apply_filters( 'status_filter', array( 'one', 'two', 'three' ) );
        // The class for targeting the url
        $span_class = apply_filters( 'status_class', 'sl-unsafe-link' );
        $modified = '';
        $the_comment = $comment;
        foreach( $arrays as $key => $array ){
            $url = $array['url'];
            $data = $array['data'];
            if( in_array( $data, $status ) ) {
                $modified = '<span style="font-weight:bold">'.$url.'</span>';
                $the_comment = str_replace( $url, $modified, $the_comment );
            }
        }
        return $the_comment;
    }
    $comment = 'Lorem www.link1.com and ipsum dolor sit amet, http://link2.com consectetur adipiscing elit. Maecenas http://link3.com eu tempor nibh.';
    $new = check_the_status($arrays, $comment);
    echo $new;

我在本地运行了它,这是输出:

Lorem <span style="font-weight:bold">www.link1.com</span> and ipsum dolor sit amet, http://link2.com consectetur adipiscing elit. Maecenas <span style="font-weight:bold">http://link3.com</span> eu tempor nibh.

最新更新