2016-12-08 67 views

回答

0

使用多維數組的一個可能的解決方案:

$string = '[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]@old|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]|[email protected]'; 
$primaryData = explode('|', $string); 
$categories = []; 
foreach($primaryData as $data) 
{ 
    $dataDetails = explode('@', $data); 
    //storing each category in a separate array 
    $categories[$dataDetails[0]][] = $dataDetails[1]; 
} 
foreach ($categories as $categoryName => $categoryContents) { 
    //we should only want unique categories 
    $categories[$categoryName] = array_unique($categoryContents); 
    //test output 
    print "$categoryName category:\n"; 
    print_r($categories[$categoryName]); 
    print "\n"; 
} 
+0

注意:字符串是動態的。 – pichanakian

+0

已更新代碼以處理任何類別類型。 –

0

請注意,該字符串是動態的。

$string1 =「type @ new | color @ red | company @ zara | weight @ 500gr」; $string2 =「type @ new | color @ red | company @ zara」; $string3 =「type @ new | color @ red | company @ zara | insurance @ yes | warranty @ no」;

例如:

$string =「型@新|彩@紅|公司@ ZARA |國@西班牙|重量@ 500GR 類型@斯托克城|彩@黃|公司@阿迪達斯|國@意大利 類型@ old | color @ black | company @ nike | country @ france type @ soon | color @ blue | company @ reebok | country @ america | insurance @ yes | warranty @ no「;