【问题标题】:How to filter and set priority arrays based on key in php如何根据php中的键过滤和设置优先级数组
【发布时间】:2020-09-05 16:52:01
【问题描述】:
Array
(
    [0] => stdClass Object
        (
            [category_id] => 5
            [distributor_id] => 999999
            [name] => Attractions
            [parent_category_id] => 0
            [is_global_category] => 0
            [category_logo] => qrcodes/categoryupload/img_1438954822_attractions.png
            [is_active] => 1
            [ticket_ids] => 
            [position] => 0
        )

    [1] => stdClass Object
        (
            [category_id] => 5
            [distributor_id] => 8885
            [name] => Attractions
            [parent_category_id] => 0
            [is_global_category] => 0
            [category_logo] => qrcodes/categoryupload/img_1438954822_attractions.png
            [is_active] => 1
            [ticket_ids] => 
            [position] => 0
        )

    [2] => stdClass Object
        (
            [category_id] => 6
            [distributor_id] => 999999
            [name] => Walk & Bike
            [parent_category_id] => 0
            [is_global_category] => 0
            [category_logo] => qrcodes/categoryupload/img_1438406863_walk-bike.png
            [is_active] => 1
            [ticket_ids] => ["13594"]
            [position] => 0
        )

)

我想从这个数组中创建一个新数组。这里有两种类型的分销商 id 可以是:

[distributor_id] => 999999 这是固定的

秒是

[distributor_id] => $cod_id(8885) 任何登录的人

现在我想创建新数组并希望优先考虑 [distributor_id] => $cod_id(8885) 并希望结果看起来像这样

Array
    (

        [0] => stdClass Object
            (
                [category_id] => 5
                [distributor_id] => 8885
                [name] => Attractions
                [parent_category_id] => 0
                [is_global_category] => 0
                [category_logo] => qrcodes/categoryupload/img_1438954822_attractions.png
                [is_active] => 1
                [ticket_ids] => 
                [position] => 0
            )

        [1] => stdClass Object
            (
                [category_id] => 6
                [distributor_id] => 999999
                [name] => Walk & Bike
                [parent_category_id] => 0
                [is_global_category] => 0
                [category_logo] => qrcodes/categoryupload/img_1438406863_walk-bike.png
                [is_active] => 1
                [ticket_ids] => ["13594"]
                [position] => 0
            )

    )

如何使用 foreach 和其他解决方案来处理它

【问题讨论】:

标签: php arrays codeigniter sorting laravel-5


【解决方案1】:

你可以使用usort()函数。

function comparator($object1, $object2) { 
    return $object1->distributor_id > $object2->distributor_id; 
} 

usort($array, 'comparator');

阅读更多来自here

【讨论】:

    【解决方案2】:

    我已经为您的问题编写了一个可能的解决方案,必要时会提到 cmets,看看它是否对您有帮助。

    假设,

    Array
    (
        [0] => stdClass Object
            (
                [category_id] => 5
                [distributor_id] => 999999
                [name] => Attractions
                [parent_category_id] => 0
                [is_global_category] => 0
                [category_logo] => qrcodes/categoryupload/img_1438954822_attractions.png
                [is_active] => 1
                [ticket_ids] => 
                [position] => 0
            )
    
        [1] => stdClass Object
            (
                [category_id] => 5
                [distributor_id] => 8885
                [name] => Attractions
                [parent_category_id] => 0
                [is_global_category] => 0
                [category_logo] => qrcodes/categoryupload/img_1438954822_attractions.png
                [is_active] => 1
                [ticket_ids] => 
                [position] => 0
            )
    
        [2] => stdClass Object
            (
                [category_id] => 6
                [distributor_id] => 999999
                [name] => Walk & Bike
                [parent_category_id] => 0
                [is_global_category] => 0
                [category_logo] => qrcodes/categoryupload/img_1438406863_walk-bike.png
                [is_active] => 1
                [ticket_ids] => ["13594"]
                [position] => 0
            )
    

    存储在一个名为$your_array的变量中,然后

    $existArr = array(); // we'll be storing data in category_id => distributor_id pair
    $newArr   = array(); // this will be the new array
    
    foreach($your_array as $your_arr){ // traverse the data(old array)
    
        if(array_key_exists($your_arr->category_id, $existArr)){ // check if key already exists
    
            if($existArr[$your_arr->category_id] == 8885){ // if the key exists, check if the value of that key is 8885(priority)
    
                continue; // if yes, skip
    
            }else{ // if not, over-write(update the value) 
    
                $existArr[$your_arr->category_id] = $your_arr->distributor_id; // or simply 8885 -- update the {$existArr}
    
                foreach($newArr as $key => $val){ // search the key of previous saved value in {$newArr}
    
                    if($your_arr->category_id == $val->category_id){ 
    
                        $newArr[$key] = $your_arr; // update the current value in {$newArr}
                        break;
    
                    }
                }
    
            }
    
        }else{ // if the key doesn't exist
    
            $existArr[$your_arr->category_id] = $your_arr->distributor_id; // or simply 8885 -- insert key => value in the {$existArr}
            $newArr = $your_arr; // insert the current value in {$newArr}
    
        }
    
    }
    print_r($newArr);
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 2020-10-12
      • 1970-01-01
      • 2017-01-13
      相关资源
      最近更新 更多