2015-09-20 31 views
2

我有一個字符串,用逗號分隔。我用爆炸來通過逗號分隔它,這給了我作爲數組的結果。在那個數組中,我有需要被分隔的數據:。我該如何實現這個?以下是字符串和輸出。任何幫助...提前感謝。siredHow我可以拆分一個逗號分隔的字符串到一個數組中,並再次在PHP中通過冒號拆分該數組?

字符串:

$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon' 
$output = explode(',', $ing); 

輸出:

Array ([0] => coconut water :3-4 cups 
    [1] => coconut flesh : ½ cup 
    [2] => tea :4 tablespoon 
    [3] => Ice cubes :24-32 
    [4] => Lemon juice :4 tablespoon 
    [5] => Honey :8 tablespoon) 

現在,我想單獨椰子水:3-4杯的結腸。

編輯:

$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon' 
$output1 = explode(',', $ing); 
$output=array(); 
foreach($output1 as $item){ 
    $output[]=explode(':', $item); 
} 

輸出:

Array 
(
[0] => Array 
    (
     [0] => coconut water 
     [1] => 3-4 cups 
    ) 

[1] => Array 
    (
     [0] => 
coconut flesh 
     [1] => ½ cup 
    ) 

[2] => Array 
    (
     [0] => 
tea 
     [1] => 4 tablespoon 
    ) 

[3] => Array 
    (
     [0] => 
Ice cubes 
     [1] => 24-32 
    ) 

[4] => Array 
    (
     [0] => 
Lemon juice 
     [1] => 4 tablespoon 
    ) 

[5] => Array 
    (
     [0] => 
Honey 
     [1] => 8 tablespoon 
    ) 

) 

所需的輸出:

<tr> 
    <td>coconut water </td> 
    <td>3-4 cups</td> 
</tr> 
<tr> 
    <td>coconut flesh</td> 
    <td>½ cup</td> 
</tr> 
<tr> 
    <td>tea </td> 
    <td>4 tablespoon</td> 
</tr> 
<tr> 
    <td>Ice cubes</td> 
    <td>24-32</td> 
</tr> 
<tr> 
    <td>Lemon juice</td> 
    <td>4 tablespoon</td> 
</tr> 
<tr> 
    <td>Honey</td> 
    <td>8 tablespoon</td> 
</tr> 
+1

'$輸出= explode(',',$ ing); array_walk($ output,function(&$ value){$ value = explode(':',$ value);});' –

+0

你想要最終結果全部在一維數組中還是可以array [0]是第二次爆炸後的數組? –

回答

2

請嘗試:

$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon' 
$output1 = explode(',', $ing); 
$output=array(); 
foreach($output1 as $item){ 
    $output[]=explode(':', $item); 
} 

要顯示段:

foreach($output as $row){ 
    //insert a new row here: 
    echo '<tr>'; 
     //fill cells in row: 
     echo '<td>'.$row[0].'</td>'; 
     echo '<td>'.$row[1].'</td>'; 
    //close the row: 
    echo '</tr>'; 
} 
+0

任何你不使用'$ output = []'的原因? –

+0

@BenMansley你的意思是? – Ormoz

+0

聲明,'$ output = array()' –

-1

如果您迭代您在使用爆炸後的陣列上,你也可以用爆炸來用冒號分隔值(數組的值)。

+2

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 – Bono

+0

我的回答有什麼問題?我的描述與接受的解決方案完全一樣,只是我沒有提供源代碼。 – SayusiAndo

0

請試試這個。這可能會奏效:)

$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon'; 
$output = explode(',', $ing); 

function getBetween($content,$start,$end) { 
    $r = explode($start, $content); 

    if (isset($r[1])){ 

     $r = explode($end, $r[1]); 
     return $r[0]; 

    } 
    return ''; 
} 


    foreach ($output as $value) { 
     # code... 

      $value = $value.'_!!_'; 
      $data = getBetween($value,':','_!!_'); 
      echo $data = str_replace('_!!_','',$data); 
      echo '<br>'; 
    } 
0
$ing = 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon'; 

$lines = explode(',', $ing); 

foreach($lines as $line) { 
    $item = explode(':', $line); 
    $tablerow[] = '<td>' . $item[0] . '</td><td>' . $item[1] . '</td>'; 
} 

echo '<table><tr>' . implode('</tr><tr>', $tablerow) . '</tr></table>'; 
相關問題