如何根据 php 中的键过滤和设置优先级数组


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和其他解决方案来处理它

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

假设

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

你可以使用 usort(( 函数。

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

从这里阅读更多内容。

相关内容

  • 没有找到相关文章

最新更新