【问题标题】:PHP: Get text separated by commasPHP:获取用逗号分隔的文本
【发布时间】:2009-04-18 19:31:52
【问题描述】:

我有一个多维数组。例如这样的东西:

Array (
    [0] => Array
        (
            [title] => Star Trek - Viaje a las estrellas
            [country] => Venezuela, Spain, long title, poster title
        )

    [1] => Array
        (
            [title] => Viaje a Las Estrellas
            [country] => Venezuela
        )
)

我想从 [country] 中获取逗号之间的文本并将每个元素插入到单独的索引中,例如:

Array (
    [0] => Array
        (
            [title] => Star Trek - Viaje a las estrellas
            [country] => [0] => Venezuela
                         [1] => Spain
                         [2] => long title
                         [3] => poster title
        )

    [1] => Array
        (
            [title] => Viaje a Las Estrellas
            [country] => Venezuela
        )
)

可能数组布局不正确,但我只是想向您解释一下我需要做什么。 请注意,并非总是 [country] 包含用逗号分隔的元素,有时只是一个元素。

我该怎么做??

谢谢!

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    尝试在国家元素上使用explode()。您可以使用", " 分隔符,因为这些是逗号分隔值。

    一种方法(类似于其他人的建议)是:

    // Assuming that $data contains your multidimensional array...
    for ($i = 0; $i < count($data); $i++)
    {
        if (strstr($data[$i]['country'], ', '))
        {
            $data[$i]['country'] = explode(', ', $data[$i]['country']);
        }
    }
    

    另外,请注意,您实际上并不需要使用 strpos()strstr() 在这里可以完美运行。

    【讨论】:

    • strpos 会更好,我没有 +。
    【解决方案2】:

    您可以使用preg_split 函数和正则表达式来拆分字符串:

    foreach ($array as $key => $item) {
        if (strpos($item['country'], ',') !== false) {  // check if string contains a comma
            $array[$key]['country'] = preg_split('/,\s*/', $item['country']);
        }
    }
    

    【讨论】:

    • 当有一个非常好的explode()函数可用时,为什么还要使用preg_split?
    • @Paolo Bergantino:也删除可能的空格字符。
    • 为什么不在explode()的分隔符中包含空格? preg_split() 在这里似乎有点矫枉过正......
    • @htw:我不知道要处理的值是从哪里来的,也不知道逗号后面是否总是跟一个空格字符。使用正则表达式可以覆盖这种不确定性。
    • 顺便问一下:为什么每个人都对正则表达式有如此根本的反感?正则表达式通常并不邪恶。
    【解决方案3】:
    $a = Array (
        0 => Array
            (
                "title" => "Star Trek - Viaje a las estrellas",
                "country" => "Venezuela, Spain, long title, poster title"
            ),
    
        1 => Array
            (
                "title" => "Viaje a Las Estrellas",
                "country" => "Venezuela"
            )
    );
    $res = array();    
        foreach($a as $k => $v){
            foreach($v as $key => $value){
                switch($key){
                    case "country":                                
                        $r = split(",", $value); 
                        foreach($r as $index => $val){
                            $res[$k][$key][$index] = trim($val);
                        }
                      break;
                    default:
                        $res[$k][$key] = $value;
                    break;
                }
            }
        }
    
        print_r($res);
    

    输出:

    Array
    (
        [0] => Array
            (
                [title] => Star Trek - Viaje a las estrellas
                [country] => Array
                    (
                        [0] => Venezuela
                        [1] => Spain
                        [2] => long title
                        [3] => poster title
                    )
            )
        [1] => Array
            (
                [title] => Viaje a Las Estrellas
                [country] => Array
                    (
                        [0] => Venezuela
                    )
            )
    )
    

    【讨论】:

    • 请注意,由于您仅使用 ',' 作为分隔符来分解字符串,因此国家数组中的三个值中有前导空格。
    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2014-03-18
    • 2020-04-04
    相关资源
    最近更新 更多