Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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?

share|improve this question
    
create another container for categories and push, either filter thru in_array or push them all and use array_unique – Ghost Feb 20 at 14:11
    
Does my post answer your question? – Jonan Feb 20 at 14:18
1  
Dear Jonan, I tried your second solution and it gave me only (1,2,3,4,5,6,7,8) from the drop down..After I look at your code I switched foreach($re_services as $category => $re_services){...... with foreach($Service_Array as $category => $re_services){ ... and worked perfectly.. Thank you so much for your help. – R.Karar Feb 20 at 15:50

1 Answer 1

up vote 1 down vote accepted

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>';
share|improve this answer
    
Replace this foreach($re_services as $category => $re_services){ ..... with...... foreach($Service_Array as $category => $re_services){ Thank you @Jonan – R.Karar Feb 20 at 15:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.