PHP数组删除重复值

时间:2015-02-20 14:01:16

标签: php arrays

我有这个多维度,我从列表中得到它。我希望该列表中的项目按“类别”分组, 这是php代码:

foreach ($re_services as $re_serv){
    $ows_Category = $re_serv->getAttribute("ows_OfferingCatType");
    $ows_ServiceID = $re_serv->getAttribute("ows_Title");
    $ows_Service = $re_serv->getAttribute("ows_OfferingsName_Edit");
    $ows_Hours = $re_serv->getAttribute("ows_Hours");

    $Service_Array []= array(
        "Category" => $ows_Category,
        "ServiceID" => $ows_ServiceID,
        "Service" => $ows_Service,
        "Hours" => $ows_Hours 
    ); }    

我的输出是这样的:

Array
(
    [0] => Array
        (
            [Category] => Styling
            [ServiceID] => Blow Dry-Male_104
            [Service] => Blow Dry-Male
            [Hours] => 1.00000000000000
        )

    [1] => Array
        (
            [Category] => Styling
            [ServiceID] => Ladies Cut & Blow Dry_101
            [Service] => Ladies Cut & Blow Dry
            [Hours] => 1.00000000000000
        )

    [2] => Array
        (
            [Category] => Styling
            [ServiceID] => Longer Blow Dry_103
            [Service] => Longer Blow Dry
            [Hours] => 2.00000000000000
        )

    [3] => Array
        (
            [Category] => Styling
            [ServiceID] => Mens Cut & Blow Dry_102
            [Service] => Mens Cut & Blow Dry
            [Hours] => 1.00000000000000
        )

    [4] => Array
        (
            [Category] => Super Services
            [ServiceID] => Half Head_106
            [Service] => Half Head
            [Hours] => 1.00000000000000
        )

    [5] => Array
        (
            [Category] => Super Services
            [ServiceID] => Highlight/Lowlights_105
            [Service] => Highlight/Lowlights
            [Hours] => 3.00000000000000
        )

    [6] => Array
        (
            [Category] => Super Services
            [ServiceID] => Luxury Hair Treatments_109
            [Service] => Luxury Hair Treatments
            [Hours] => 4.00000000000000
        )

    [7] => Array
        (
            [Category] => Technical
            [ServiceID] => Bridal Hair_108
            [Service] => Bridal Hair
            [Hours] => 4.00000000000000
        )

    [8] => Array
        (
            [Category] => Technical
            [ServiceID] => Hair Up_107
            [Service] => Hair Up
            [Hours] => 1.00000000000000
        )

)

如您所见,我有3个类别(样式,超级服务,技术)。现在我需要的输出是html标签,其值如下:

<select> 
<optgroup label="Category">
    <option value="ServiceID">Service Hours</option>
</optgroup> </select>

但是没有带有类别标签的任何重复。我该如何实现呢?

1 个答案:

答案 0 :(得分:1)

试试这个:

$categories = array();

foreach ($re_services as $re_serv){
    $ows_Category = $re_serv->getAttribute("ows_OfferingCatType");
    $ows_ServiceID = $re_serv->getAttribute("ows_Title");
    $ows_Service = $re_serv->getAttribute("ows_OfferingsName_Edit");
    $ows_Hours = $re_serv->getAttribute("ows_Hours");

    $Service_Array []= array(
        "Category" => $ows_Category,
        "ServiceID" => $ows_ServiceID,
        "Service" => $ows_Service,
        "Hours" => $ows_Hours 
    ); 

    //add the following part:
    if(!in_array($ows_Category, $categories))
        $categories[] = $ows_Category;
}

现在,您可以使用foreach上的$categories循环打印出正确的html标记:

echo '<select>';
foreach($categories as $category){
    echo '<optgroup label="' . $category . '">';
    foreach ($re_services as $re_serv){
        if($re_serv['Catergory'] == $category)
            echo '<option value="' . $re_serv['ServiceID'] . '">' . $re_serv['Service'] . '</option>';
    }
    echo '</optgroup>';
}
echo '</select>';

另一个选择是彻底改变你的数组结构:

foreach ($re_services as $re_serv){
    $ows_Category = $re_serv->getAttribute("ows_OfferingCatType");
    $ows_ServiceID = $re_serv->getAttribute("ows_Title");
    $ows_Service = $re_serv->getAttribute("ows_OfferingsName_Edit");
    $ows_Hours = $re_serv->getAttribute("ows_Hours");

    $Service_Array[$ows_Category][]= array(
        "ServiceID" => $ows_ServiceID,
        "Service" => $ows_Service,
        "Hours" => $ows_Hours 
    );
}

echo '<select>';
foreach($Service_Array as $category => $re_services){
    echo '<optgroup label="' . $category . '">';
    foreach($re_services as $re_serv){
        echo '<option value="' . $re_serv['ServiceID'] . '">' . $re_serv['Service'] . '</option>';
    }
    echo '</optgroup>';
}
echo '</select>';
相关问题