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

)

i want to create new array from this array.Here two type of distributor id can come one is :

[distributor_id] => 999999 this is fixed

second is

[distributor_id] => $cod_id(8885) any person who is login

Now i want to create new array and want to give priority to [distributor_id] => $cod_id(8885) and want result look like this

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
            )

    )

How to handle it using foreach and other solution

Dum
  • 1,431
  • 2
  • 9
  • 23
Shahzad Intersoft
  • 762
  • 2
  • 14
  • 35

2 Answers2

0

I've written a possible solution for your issue, comments are mentioned wherever necessary, see if it helps you.

Suppose,

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
        )

is stored in a variable named $your_array, then

$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);
sauhardnc
  • 1,961
  • 2
  • 6
  • 16
0

You can use usort() function.

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

usort($array, 'comparator');

Read more from here.

Dum
  • 1,431
  • 2
  • 9
  • 23