R.Karar
R.Karar

Reputation: 41

PHP array delect duplicate values

I have this multidimensional that I'm getting it from a list. I want the items in that list to be grouped by the "Category", This is the php code:

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

And my output is this:

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
        )

)

As you can see I have 3 categroies (Styling, Super Services, Technical ). Now the output I need is html tags with values like this:

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

But with out any duplicates with the Category lable. How can I achive that?

Upvotes: 0

Views: 72

Answers (1)

Jonan
Jonan

Reputation: 2536

Try this:

$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;
}

Now, you can use a foreach loop on $categories to print out the right html tags:

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>';

Another option is to completely change your array structure:

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>';

Upvotes: 1

Related Questions