PHP数组delect重复的值 [英] PHP array delect duplicate values

查看:197
本文介绍了PHP数组delect重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种多维的,我从列表中得到它。我想在该列表中的项目由类别被分组,
这是PHP code:

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

和我的输出是这样的:


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个categroies(造型,超级服务,技术)。现在我需要输出为html标签,像这样的值:

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>

但随着脱离与分类拉布勒任何重复。我怎样才能achive呢?

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

推荐答案

试试这个:

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

现在,您可以使用 $类别来打印出正确的HTML标记的foreach 循环:

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

这篇关于PHP数组delect重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆