PHP: Metin virgülle ayırarak alın

3 Cevap php

Ben örneğin bir multidimensinal dizi .. Böyle bir şey var:

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
        )
)

I [ülke] adlı virgüller arasında te metin almak ve örneğin, ayrı indeksler her bir öğe eklemek istiyorum:

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
        )
)

Probably the array layout is incorrect but I just want to explain to you what I need to do. Note that not always [country] contains elements separated by commas, sometimes is just one single element.

Bunu nasıl yapabilirim?

Teşekkürler!

3 Cevap

Bu virgülle ayrılmış değerler olduğundan, explode() on the country element. You can use a separator of ", " kullanmayı deneyin.

(Başkalarının önerdi nasıl benzer olan) bunu yapmak için bir yol olacaktır:

// 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']);
    }
}

strstr() burada mükemmel çalışıyor - Ayrıca, gerçekten kullanmak için strpos() gerekmez unutmayın.

Sen dize bölmek için preg_split fonksiyonu ve normal ifadeyi kullanabilirsiniz:

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

çıktı:

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
                )
        )
)