2012-10-05 112 views
1

我有這樣的字符串(只有它的例子):陣列操作

$string='cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0'; 

和代碼重拍此字符串

$string=explode(",", $string); 
$i = 0; 
$ile=count($string); 
while ($i < $ile) { 
    $a = explode("-", $string[$i]); 
    if($a[2]==0) $animal .= $a[0].' '.$a[1]; 
    elseif($a[2]==1) $thing .= $a[0].' '.$a[1]; 
    $i++; 
} 

我需要搜索這個數組和分組在:$動物和$件事 我知道爲什麼這段代碼不工作,但我沒有任何想法做到這一點。請幫我:)

+0

你應該考慮使用更多的自我解釋可變名。這種編碼方式不會讓你自己更容易。 – Ben

+0

預期產量是多少? –

+0

預期輸出是基本需要給你想法如何做到這一點。 –

回答

0

您還可以通過在案件使用preg_match實現它時,你的形象的名字包含其他字符,如額外-或數字,這裏是例子:

$string = 'cat-cat.png-0,dog-snoopy-dog.jpg-0,phone-nokia-6310.png-1,rabbit-bugs-bunny-eating-carrot.png-0,ignore-me'; 

$tmp = explode(',', $string); 

$animals = $things = array(); 

foreach($tmp as $value) 
{ 
    preg_match('/(?P<name>[A-Za-z]+)-(?P<file>[A-Za-z0-9\.-]+)-(?P<number>\d+)/', $value, $matches); 

    if($matches) 
    { 
     switch($matches['number']) 
     { 
      case '0': 
       array_push($animals, sprintf('%s %s', $matches['name'], $matches['file'])); 
       break; 
      case '1': 
       array_push($things, sprintf('%s %s', $matches['name'], $matches['file'])); 
       break; 
     } 
    } 
} 

結果:

array (size=3) 
    0 => string 'cat cat.png' (length=11) 
    1 => string 'dog snoopy-dog.jpg' (length=18) 
    2 => string 'rabbit bugs-bunny-eating-carrot.png' (length=35) 
array (size=1) 
    0 => string 'phone nokia-6310.png' (length=20) 
0

這應該完成的任務:

$string = 'cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0'; 
$array = explode(',', $string); 
$animal = array(); 
$thing = array(); 
foreach ($array as $item) 
{ 
    $a = explode('-', $item); 
    if($a[2] == 1) 
    { 
     $thing[] = $a[0] . ' ' . $a[1]; 
    } 
    else 
    { 
     $animal[] = $a[0] . ' ' . $a[1]; 
    } 
} 

然後從那裏你可以,如果你需要他們作爲字符串破滅這些陣列。

0

如果您舉了一個代碼應該輸出的例子,或者您希望實現的結果數組,那麼它可能會有所幫助。

如果你絕對堅持使用輸入字符串格式,那麼你可以使用正則表達式來使它更健壯。類似:

/([^,] *) - (\ d +)/

將匹配文件名和類型,假設類型始終是一個數字。

因此,例如:

preg_match_all("/([^,]*)-(\d+)/", "cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0", $matches); var_dump($matches); 
$items = array(); 
foreach ($matches[2] as $key=>$type) { 
    if (empty($items[$type])) { 
     $items[$type] = array(); 
    } 
    $items[$type][] = $matches[1][$key]; 
} 
var_dump($items); 

理想情況下,你會希望多增加一些錯誤檢查,如按預期匹配返回檢查。

1

您可以嘗試

$string = 'cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0'; 
$array = explode(",", $string); 
$list = array(); 

foreach ($array as $info) { 
    list($prifix, $name, $id) = explode("-", $info); 
    $list[$id][] = $prifix . "-" . $name; 
} 

var_dump($list); 

輸出

array 
    0 => 
    array 
     0 => string 'cat-cat.png' (length=11) 
     1 => string 'dog-dog.jpg' (length=11) 
     2 => string 'horse-xyz.png' (length=13) 
    1 => 
    array 
     0 => string 'phone-nokia.png' (length=15) 
0

試試下面的代碼

// Remove all things and all numbers with preceding hyphens 
$animal = trim(preg_replace('/([a-zA-Z\-\.]+\-1,?)|(\-\d+)/', '', $string), ','); 
// gives 'cat-cat.png,dog-dog.jpg,horse-xyz.png' 

// Remove all animals and all numbers with preceding hyphens 
$thing = trim(preg_replace('/([a-zA-Z\-\.]+\-0,?)|(\-\d+)/', '', $string), ','); 
// gives 'phone-nokia.png'